Created
December 12, 2017 21:16
-
-
Save youchen/2459e8e5fdb48aa092cb7759cfc97c6d to your computer and use it in GitHub Desktop.
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 fibonacci() func() int { | |
fib1, fib2 := 0, 1 | |
return func() int{ | |
defer func() { fib1, fib2 = fib2, fib1 + fib2 } () | |
return fib1 | |
} | |
} | |
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