What are 'Loop Variable' semantics changes in Go 1.22, and what bug did they solve?

Last updated 20 June 2026

  • Since Go 1.22, each for loop 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.mod declares 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
All Golang interview questions