2 ms·
The Go Beartrap Lying in the Shadows
- mholt 12y agoFrom the Go language spec[1]: > 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 https://golang.org/ref/spec#Short_variable_declarations
- xkarga00 12y agoActually 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.
- kxo 12y ago> It takes the best of C++, Java and Python … and then it really tightens everything up. "in 2004" belongs in that statement.