Python to Go Cheatsheet
353.solutions
Python to Go Cheatsheet
1–8 of 8 posts
Re: Python to Go Cheatsheet
#2Re: Python to Go Cheatsheet
#3Re: Python to Go Cheatsheet
#4My god is go a cumbersome language. I get why the explicitly returned errors are a good thing but the complexity for parsing json surprises me
Re: Python to Go Cheatsheet
#5My god is go a cumbersome language. I get why the explicitly returned errors are a good thing but the complexity for parsing json surprises me
// example for unmarshalling an http request to JSON
contents, err := ioutil.Readall(req.Body)
if err != nil { return err }
req.Body.Close()
user := new(User)
if err := json.Unmarshal(contents, user); err != nil { return nil }
// do whatever you want with 'user'
Re: Python to Go Cheatsheet
#6My god is go a cumbersome language. I get why the explicitly returned errors are a good thing but the complexity for parsing json surprises me
What? // example for unmarshalling an http request to JSON contents, err := ioutil.Readall(req.Body) if err != nil { return err } req.Body.Close() user := new(User) if err := json.Unmarshal(contents, user); err != nil { return nil } // do whatever you want with 'user'
Re: Python to Go Cheatsheet
#7My god is go a cumbersome language. I get why the explicitly returned errors are a good thing but the complexity for parsing json surprises me
What? // example for unmarshalling an http request to JSON contents, err := ioutil.Readall(req.Body) if err != nil { return err } req.Body.Close() user := new(User) if err := json.Unmarshal(contents, user); err != nil { return nil } // do whatever you want with 'user'
Does Go have something analogous to Rust's `try!` or `?` operator, which would turn it into (pseudocode):
contents, err := try!(ioutil.Readall(req.Body))
Or: contents, err := ioutil.Readall(req.Body)?
If not, why not?(I don't know Go)
Re: Python to Go Cheatsheet
#8Earlier quoted context omitted.
What? // example for unmarshalling an http request to JSON contents, err := ioutil.Readall(req.Body) if err != nil { return err } req.Body.Close() user := new(User) if err := json.Unmarshal(contents, user); err != nil { return nil } // do whatever you want with 'user'
Why do you have to call `req.Body.Close()`? Does Go not have RAII? Does Go have something analogous to Rust's `try!` or `?` operator, which would turn it into (pseudocode): contents, err := try!(ioutil.Readall(req.Body)) Or: contents, err := ioutil.Readall(req.Body)? If not, why not? (I don't know Go)
As for why not, you'd have to ask the Go team. But my guess would be that as a general rule, Go does not have language feature X. It's a very, very basic language deliberately lacking pretty much every modern language feature you can think of. The pro/con is that programming in Go is very delibarete. Go programs will only ever do what you explicitly make them do, to an almost ridiculous extent. If a Go program knows how to sort a list of customers, it takes 20 lines of code to make it aware of how to sort any other record type. Especially operations minded people this is a big plus.