A common mistake that I have often seen cause exasperation to many programmers is that of accidental variable shadowing, this is one of the first things that I look for when asked to help debug misbehaving code. This occurs via misuse of the short variable declaration clause :=.
Let's recap declaration, assignment and what the shorthand does:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var x int // declaration | |
x = 1 // assignment | |
// declaration and assigment via shorthand, integer type inferred from value | |
x := 1 |
Now when does this get us into trouble? The problem occurs when we accidentally declare and assign to a new variable in a different scope rather than assign to an existing variable in an outer scope.
'Short cuts make long delays' - Frodo Baggins
https://play.golang.org/p/Pset590PA2
This example demonstrates an accidental variable shadowing bug in code used to determine the highest integer in a slice. The variable 'highest' is redeclared and assigned to in the scope of the if statement, shadowing the 'highest' variable declared in the scope of main. Whereas desired behaviour is assignment to the highest variable declared in the scope of the main function. Here we say that the variable highest is 'shadowed' as a result of this redeclaration. Try modifying line 14 of the code to 'highest = v' and note the change in behaviour.
Now, it is a good question as to why this is allowed, I believe that it is primarily to allow flexibility and to protect existing code from things like imports as explained here.
This is catchable by using go vet with the experimental option -shadow enabled.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
aulty@aulty-xps13 ~/go/src/github.com/cocoonlife/go $ go tool vet -shadow ~/Desktop/go_sandbox/shadowing.go | |
/home/aulty/Desktop/go_sandbox/shadowing.go:14: declaration of "highest" shadows declaration at /home/aulty/Desktop/go_sandbox/shadowing.go:10 |
For those interested, more in depth discussion can be seen here.
No comments:
Post a Comment