mirror of
https://github.com/hanzoai/yaegi.git
synced 2026-10-01 00:49:47 +00:00
In the case of a Go short definition (i.e. `a, b := f()`), the new defined variables must be (re-)created in order to preserve the previous value (if in a loop) which can be still in use in the context of a closure. This must not apply to redeclared variables which simply see their value reassigned. The problem was both occuring in callBin() for runtime functions and assignFromCall() for functions created in the interpreter. Fixes #1497.
37 lines
478 B
Go
37 lines
478 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
)
|
|
|
|
type monkey struct {
|
|
test func() int
|
|
}
|
|
|
|
func main() {
|
|
input := []string{"1", "2", "3"}
|
|
|
|
var monkeys []*monkey
|
|
|
|
for _, v := range input {
|
|
kong := monkey{}
|
|
divisor, err := strconv.Atoi(v)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
fmt.Print(divisor, " ")
|
|
kong.test = func() int {
|
|
return divisor
|
|
}
|
|
monkeys = append(monkeys, &kong)
|
|
}
|
|
|
|
for _, mk := range monkeys {
|
|
fmt.Print(mk.test(), " ")
|
|
}
|
|
}
|
|
|
|
// Output:
|
|
// 1 2 3 1 2 3
|