You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This program
http://play.golang.org/p/RXFJJh5KRw
reports an error as expected, albeit a confusing one. This is related to, or the same as
issue #6764.
A short variable declaration in a for range loop can never redeclare an existing
variable because a for statement opens a new scope and thus there are no variables
declared earlier in that scope. So there should be a double declaration error.
Interestingly, adding an external declaration appears to "fix" the problem:
http://play.golang.org/p/4spddNy6Ki
The code compiles and runs without errors. Output:
0
1
2
3
Here it looks like the short variable declaration actually was handled like a
redeclaration of the outer a. Interestingly, value assigned to a is the index and not
the indexed element. Contrast this (and its output) to the following correct program:
http://play.golang.org/p/BWZD1fCWKj
Output:
1
2
3
3
The text was updated successfully, but these errors were encountered:
rsc
changed the title
cmd/gc: invalid for range loop executes w/o error message
cmd/compile: invalid for range loop executes w/o error message
Jun 8, 2015
The text was updated successfully, but these errors were encountered: