Statement
Per-iteration loop variables
In Go 1.22 or later language versions, loop-declared iteration variables have separate instances per iteration. Assignment with = to a preexisting variable still reuses it. Older module language versions differ. Example output: '2 4 6'.
Formal Syntax & Structure
go syntaxgo
for _, v := range values { ... }Working Example
Example Snippetgo
package main
import "fmt"
func main() { var fs []func() int for _, v := range []int{2, 4, 6} { fs = append(fs, func() int { return v }) } fmt.Println(fs[0](), fs[1](), fs[2]())}