The first change forces a variable definition to reallocate a new memory slot to avoid corrupting a previously defined one in a loop block. The second change ensures that the frame clone operations obtains a copy of the original data slice, to preserve the original context set in a loop. Fixes #1035.
26 lines
295 B
Go
26 lines
295 B
Go
package main
|
|
|
|
import "fmt"
|
|
|
|
type T struct {
|
|
F func()
|
|
}
|
|
|
|
func main() {
|
|
foos := []T{}
|
|
|
|
for i := 0; i < 3; i++ {
|
|
a := i
|
|
n := fmt.Sprintf("i=%d", i)
|
|
foos = append(foos, T{func() { println(i, a, n) }})
|
|
}
|
|
foos[0].F()
|
|
foos[1].F()
|
|
foos[2].F()
|
|
}
|
|
|
|
// Output:
|
|
// 3 0 i=0
|
|
// 3 1 i=1
|
|
// 3 2 i=2
|