Skip to content

Instantly share code, notes, and snippets.

@HendrixString
Last active February 13, 2017 14:11
Show Gist options
  • Save HendrixString/b032016224849993a13e59c2878b69b8 to your computer and use it in GitHub Desktop.
Save HendrixString/b032016224849993a13e59c2878b69b8 to your computer and use it in GitHub Desktop.
Golang Fibonnaci
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
var n, f_n_1, f_n_2 = -1, 1, 1
return func() int {
n++
if n == 0 {
return f_n_2
} else if n == 1 {
return f_n_1
}
f_n_2, f_n_1 = f_n_1, f_n_2 + f_n_1
return f_n_1
}
}
func main() {
next := fibonacci()
for i := 0; i < 10; i++ {
fmt.Println(next())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment