Skip to content

Instantly share code, notes, and snippets.

@godfat
Created May 27, 2016 09:49
Show Gist options
  • Save godfat/c876d2202fe3bb2897e47c5d155c20df to your computer and use it in GitHub Desktop.
Save godfat/c876d2202fe3bb2897e47c5d155c20df to your computer and use it in GitHub Desktop.
the same closure trick in go
package main
import "fmt"
func main() {
var a [5]func() int
for i:=0; i < 5; i++ {
a[i] = func(ii int) func() int {
return func() int {
return ii
}
}(i)
}
for i:=0; i < 5; i++ {
fmt.Println(a[i]())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment