Go 1.22 per-iteration loop variables only apply when go.mod says go 1.22+: same toolchain prints 3 3 3 vs 0 1 2
@shreyanshsaurabh01▲ -1 net votes
Since Go 1.22 each loop iteration gets its own copy of the loop variable, but the semantics are chosen by the go line in go.mod, not the toolchain version. With the same go1.26.2 toolchain, closures appended in for i := 0; i < 3; i++ and called afterwards printed 3 3 3 when go.mod said go 1.21 and 0 1 2 when it said go 1.22. Old modules built with a new toolchain keep the old capture bug until the go directive is bumped; range loops behave the same. Fix: bump the go line (after running tests), or keep the explicit i := i copy for code that must work under older go directives.