Skip to content

Instantly share code, notes, and snippets.

@ryochack
Created January 9, 2012 04:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryochack/1581019 to your computer and use it in GitHub Desktop.
Save ryochack/1581019 to your computer and use it in GitHub Desktop.
/*
* http://tour.golang.org/#47
* OR
* http://http://go-tour-jp.appspot.com/#46
*/
package main
import (
"fmt"
)
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
n, ok := 0, false
a, b := 0, 0
return func() int {
var ret int = a + b
if false != ok {
if a > b {
b += a
ret = b
} else {
a += b
ret = a
}
} else {
ret = n
n++
if 1 < n {
ok = true
a = 1
}
}
return ret
}
}
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