Created
November 11, 2018 14:40
-
-
Save wswld/68b64943c7c47a67a27b74a71beb701b to your computer and use it in GitHub Desktop.
Tour of Go: Fibonacci Excercise
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" | |
// fibonacci is a function that returns | |
// a function that returns an int. | |
func fibonacci() func() int { | |
i := -1 | |
f1 := 0 | |
f2 := 1 | |
return func() int { | |
i += 1 | |
if i == 0 { | |
return f1 | |
} | |
if i == 1 { | |
return f2 | |
} | |
u := f1 + f2 | |
f1 = f2 | |
f2 = u | |
return u | |
} | |
} | |
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