Created
November 15, 2015 04:23
-
-
Save aalvesjr/47f91aa0e667e25c49cc to your computer and use it in GitHub Desktop.
Fibonacci 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 fib() func() int { | |
a, b := 0, 1 | |
return func() int { | |
a, b = b, a+b | |
return a | |
} | |
} | |
func main() { | |
f := fib() | |
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