Last active
January 1, 2016 06:49
-
-
Save sharewind/8107925 to your computer and use it in GitHub Desktop.
golang closure
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" | |
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