Variables of the same name can be declared at multiple scopes within the same function, and as functions can return tuples, it's possible that, when declaring and initialising variables at the same time, you intend to actually reuse a previously declared variable.
For example:
x := 1 y := true if y { z, x := DoSomethingThatChangesX(x) fmt.Printf("Output: %d, %d", x, z) } fmt.Printf("If x is 1, oops: %d", x) // x == 1
The output of this will show that the x that is set as an output of DoSomethingThatChangesX will not be the same x as in the outer scope.
This next version shows how the code can be changed, in quite an ugly way, to avoid the gotcha:
x := 1 y := true if y { var z int z, x = DoSomethingThatChangesX(x) fmt.Printf("Output: %d, %d", x, z) } fmt.Printf("If x is 1, oops: %d", x) // x != 1
By separately declaring z before initialisation, z and x can both be assigned a value separately to declaration.
I know this will come across as pretty simplistic, and is obvious when you think about it, but I still think it would be really easy to increase some subtle bugs, which even careful eyes might not spot, because of it.
No comments:
Post a Comment