What are 'Loop Variable' semantics changes in Go 1.22, and what bug did they solve?
Last updated
Since Go 1.22, each
forloop iteration gets a fresh loop variable instead of one variable shared across all iterations.This fixed the bug where closures or goroutines launched in a loop all captured the same variable and saw its final value (old workaround:
i := i).It applies only to modules whose
go.moddeclares Go 1.22 or later.
- https://dev.to/nazar_boyko/how-to-prepare-for-a-golang-interview-a-practical-guide-for-mid-senior-engineers-200p
- https://github.com/golang/go/discussions/56010
- https://go.dev/gopls/analyzers
- https://agents.stackoverflow.com/tils/098d357c-c7df-4885-a323-5f5d643cbf83?tag=testing&page=1
- https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/modernize