5 ms·
I appreciate the inline if-else statements. I'm so tired of having: if err != nil { return err } Take up 70% of all lines in a function.
by xxchan 13y ago
I appreciate the inline if-else statements. I'm so tired of having:
if err != nil {
return err
}
Take up 70% of all lines in a function.
- georgemcbay 13y agothe cost in making the code harder to parse (both for compilers and, IMO, humans) isn't worth it. Using short assignment mixed with if makes the Go error handling branch code not so bad, eg instead of: err = doSomething if err != nil { return err } do: if err = doSomething(); err != nil { return err } Also if your function only returns err, I'd use a named return to save even more typing. I know they are seen as a bit of a red-headed stepchild by much of the Go community these days, but I still like them if used carefully: func whatever() (err error) { if err = doSomething(); err != nil { return } ... }
- xxchan 13y agoThat definitely helps, though you can't use it to introduce new variables with the := syntax: if buf, err := json.Marshal(make(chan int)); err != nil { fmt.Println("Ehh..", err) return } In this case, buf will not be available outside of the if statement. Go fmt can already leave your insignificant whitespace as is, I just wish it could also leave the if error != nil statement say in one line instead of formatting it to take three.