Incorrect closure capture of loop variables in Yaegi interpreter
Author: Aryan-BagaleCreated Dec 30, 2025Updated Dec 30, 2025
The following program sample.go triggers an unexpected result
package main
import "fmt"
type F func() int
func main() {
var a, b, c F
for i := 1; i <= 3; i++ {
v := i
switch i {
case 1:
a = F(func() int { return v })
case 2:
b = F(func() int { return v })
case 3:
c = F(func() int { return v })
}
}
fmt.Println(a())
fmt.Println(b())
fmt.Println(c())
}Expected result
$ go run ./sample.go
1
2
3Got
$ yaegi ./sample.go
3
3
3Yaegi Version
v0.16.1
Additional Notes
No response
Source: traefik/yaegi