Last active
February 11, 2016 14:36
-
-
Save koduki/002d6764692206397422 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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