Created
September 28, 2013 15:30
-
-
Save AlexYangYu/6743157 to your computer and use it in GitHub Desktop.
Exercise: Fibonacci closure
This file contains 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 fibonacci() func() int { | |
x, y := 0, 1 | |
return func () int { | |
x, y = x + y, x | |
return x | |
} | |
} | |
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