Last active
December 30, 2015 02:29
-
-
Save ninehills/7763221 to your computer and use it in GitHub Desktop.
fibonacci 闭包 by 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
package main | |
import "fmt" | |
// fibonacci 函数会返回一个返回 int 的函数。 | |
func fibonacci() func() int { | |
sum1 := -1 | |
sum2 := 1 | |
return func() int { | |
sum := sum1 + sum2 | |
sum1 = sum2 | |
sum2 = sum | |
return sum | |
} | |
} | |
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