Re:Golang landmines
by oppositelock on Sunday September 18, 2016 @02:27AM
(#12524203)
I've been doing quite a bit of Go programming for a while now, and I've internalized all these behaviors, but this one gets me every time:
It's particularly annoying because if both foo and err are defined in the outer scope, you get a compiler error that no new variables are on the left, so you tend to forget that removing one shadows the other.
[ Parent
| on HN ]
Re:Golang landmines
by omginternets on Sunday September 18, 2016 @01:10PM
(#12526086)
I think Go is one of those languages that really benefits from an editor that performs static analysis.
I use Atom and go-metalinter catches this on a daily (if not hourly) basis. Variable shadowing is a sharp edge of the language that's very effectively dulled by proper tooling.
Not sure it would help you but adding "go vet -shadow" to some kind of test or build script may help you detect these problems.
This.
If anyone is using VIM or Atom please please please install the Go helpers, too. Atom's go-plus is awesome.
The team I work with- I beg them to use linters and go-plus and avoid these pitfalls like shadowing err. Instead most of them will run the linter, not fix the errors, then defend poor practices like shadowing and returning private types from exported methods.
Re:Golang landmines
by paulsutter on Sunday September 18, 2016 @01:58PM
(#12526303)
Declaration assignments should be more explicit. For example:
foo, err ::= someFunction() // explicitly declare both
foo, err := someFunction() // declare only foo, err must exist
Then you get all the convenience without surprises.