Go 1.22 Loop Variables: The Fix Is Versioned by Your go.mod
Go 1.22 changed the semantics of loop variables declared by a for loop: each iteration gets its own variable. This removes a well-known class of closure and goroutine bugs where work started in a loop observes the variable after a later iteration has changed it.
The easy-to-miss detail is that the new semantics depend on the module’s Go language version. Installing a newer compiler does not automatically change every older module’s behavior. The go directive in go.mod determines the language version for the package, so the migration begins with an intentional version update and a review of the compatibility notes.
Look for loops that start goroutines, register callbacks, or take addresses of the iteration variable. For modernized code, the old defensive pattern that creates a new local copy can become redundant. But do not remove it mechanically across modules with older language versions. Keep tests for concurrent work and inspect closures that capture composite state rather than only a loop index.
The change is a good example of Go’s compatibility strategy: the language can fix a common bug while projects adopt the new behavior at a controlled point. Treat the module directive as part of the source contract and update it through review, not as incidental metadata changed by a developer’s local toolchain.
The Go 1.22 release notes describe the semantics and tooling. The Go team’s loop variable preview article explains the bug pattern that motivated the change.