Closed
Description
Go version
x/tools/gopls v0.18.1
Output of go env
in your module/workspace:
irrelevant
What did you do?
This program prints 1, 2, 3 4:
func main() {
s := []int{1, 2, 3}
for i := 0; i < len(s); i++ {
fmt.Println(s[i])
if s[i] == 2 {
s = append(s, 4)
}
}
}
What did you see happen?
Gopls wants to modernize the for loop to for i := range len(s)
.
Since the range expression is evaluated only once instead of each time through the loop, the semantics change, and the program prints 1, 2 3.
What did you expect to see?
Behavior unchanged.