github.com/traefik/yaegi@v0.15.1/_test/closure13.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  )
     7  
     8  type monkey struct {
     9  	test func() int
    10  }
    11  
    12  func main() {
    13  	input := []string{"1", "2", "3"}
    14  
    15  	var monkeys []*monkey
    16  
    17  	for _, v := range input {
    18  		kong := monkey{}
    19  		divisor, err := strconv.Atoi(v)
    20  		if err != nil {
    21  			panic(err)
    22  		}
    23  		fmt.Print(divisor, " ")
    24  		kong.test = func() int {
    25  			return divisor
    26  		}
    27  		monkeys = append(monkeys, &kong)
    28  	}
    29  
    30  	for _, mk := range monkeys {
    31  		fmt.Print(mk.test(), " ")
    32  	}
    33  }
    34  
    35  // Output:
    36  // 1 2 3 1 2 3