2 ms·
As the sibling says, this is just a semantic change in the scope behaviour of loop variables. You need to add an extra line of code to emulate Go 1.22 semantics
by foldr 2y ago
As the sibling says, this is just a semantic change in the scope behaviour of loop variables. You need to add an extra line of code to emulate Go 1.22 semantics in Go 1.21 code, but there's not really any hidden magic if you just interpret Go 1.22 code according to its own defined semantics.
- tapirl 2y agoI know that, the change of "for-range" is good. But it creates more surprises in the uses of "for;;" loops. Please read the cases in that article.
- foldr 2y agoIt's a change in semantics. So it's surprising if you don't know about the change, and not surprising if you do know about it. I'm familiar with the kind of code that behaves differently under the new scoping rules. Looking at the examples in the post, I don't really share the intuition that the behavior is any more or less surprising for classic 'for' loops as opposed to 'for...range' loops.
- tapirl 2y agoIf you can guarantee that every gophers know the effects of the change. :D The problem of the change is that, if you upgrade your go version in go.mod files, the behavior of the your old code might change. And the change might be not easily found in time. I indeed haven't found any surprising cases caused by the change of "for-range".