Created
November 16, 2014 08:31
-
-
Save wangyangkobe/80bb87da159dea1dfc42 to your computer and use it in GitHub Desktop.
Go语言的“斐波纳契闭包”
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
import "fmt" | |
// fibonacci 函数会返回一个返回 int 的函数。 | |
func fibonacci() func() int { | |
a := -1 | |
b := 1 | |
return func() int { | |
a, b = b, a+b | |
return b | |
} | |
} | |
func main() { | |
f := fibonacci() | |
for i := 0; i < 10; i++ { | |
fmt.Println(f()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment