Skip to content

Instantly share code, notes, and snippets.

@sharewind
Last active January 1, 2016 06:49
Show Gist options
  • Save sharewind/8107925 to your computer and use it in GitHub Desktop.
Save sharewind/8107925 to your computer and use it in GitHub Desktop.
golang closure
package main
import "fmt"
func intSeq() []func() int {
i := 0
var result = make([]func() int, 10)
for ; i < 10; i++ {
result[i] = func() int {
i += 1
return i
}
}
return result
}
func intSeq2() func() int {
i := 0
return func() int {
i += 1
return i
}
}
func main() {
funcs := intSeq()
for _,f := range funcs{
fmt.Println(f())
}
fmt.Println("func2--------")
f2 := intSeq2()
for i:=0; i<10; i++{
fmt.Println(f2())
}
f3 := intSeq2()
fmt.Println(f3())
fmt.Println("Hello, playground")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment