Skip to content

Instantly share code, notes, and snippets.

@koduki
Last active February 11, 2016 14:36
Show Gist options
  • Save koduki/002d6764692206397422 to your computer and use it in GitHub Desktop.
Save koduki/002d6764692206397422 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"strconv"
)
func main() {
xs := []int{2, 3, 4}
var cmds []func()
for _, x := range xs {
cmds = append(cmds, func() { fmt.Println(x) })
fmt.Println("append: " + strconv.Itoa(x))
for _, cmd := range cmds {
fmt.Print("func pointer: ")
fmt.Print(cmd)
fmt.Print(", func eval: ")
cmd()
}
}
}
[koduki]$ go run example_lambda.go
append: 2
func pointer: 0x4012e0, func eval: 2
append: 3
func pointer: 0x4012e0, func eval: 3
func pointer: 0x4012e0, func eval: 3
append: 4
func pointer: 0x4012e0, func eval: 4
func pointer: 0x4012e0, func eval: 4
func pointer: 0x4012e0, func eval: 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment