Skip to content

Instantly share code, notes, and snippets.

@wswld
Created November 11, 2018 14:40
Show Gist options
  • Save wswld/68b64943c7c47a67a27b74a71beb701b to your computer and use it in GitHub Desktop.
Save wswld/68b64943c7c47a67a27b74a71beb701b to your computer and use it in GitHub Desktop.
Tour of Go: Fibonacci Excercise
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