The Go Beartrap Lying in the Shadows(qureet.com)
qureet.com
The Go Beartrap Lying in the Shadows
http://www.qureet.com/blog/golang-beartrap/?ref=hacker_news_share
2 comments
Actually the problem here has to do with the scope where the declaration happens. If redeclaration happened as mentioned in your quoted text ie. assignment of a new value, there would be no problem. As is [in a different scope], a redeclaration of err is actually a declaration of a shadowed err variable.
> It takes the best of C++, Java and Python … and then it really tightens everything up.
"in 2004" belongs in that statement.
"in 2004" belongs in that statement.
> Unlike regular variable declarations, a short variable declaration may redeclare variables provided they were originally declared earlier in the same block with the same type, and at least one of the non-blank variables is new. As a consequence, redeclaration can only appear in a multi-variable short declaration. Redeclaration does not introduce a new variable; it just assigns a new value to the original.
So if this is something you're not used to,it might be a good idea to avoid multi-variable short declarations in critical code. (Declare with `var` beforehand.)
Also, for more credibility in writing, call it Go, not golang. #golang is just a hashtag (and domain name).
[1]: https://golang.org/ref/spec#Short_variable_declarations