Skip to content

Instantly share code, notes, and snippets.

@kamatama41
Created August 27, 2014 06:21
Show Gist options
  • Save kamatama41/ec96ceb09772c2eefc62 to your computer and use it in GitHub Desktop.
Save kamatama41/ec96ceb09772c2eefc62 to your computer and use it in GitHub Desktop.
My answer of "a tour of Go #44( http://go-tour-jp.appspot.com/#44 )"
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
now, next := 1, 0
return func() int {
new_now, new_next := next, now+next
now, next = new_now, new_next
return next
}
}
func main() {
f := fibonacci()
for i := 0; i < 10; i++ {
fmt.Println(f())
}
}
// result
//1
//1
//2
//3
//5
//8
//13
//21
//34
//55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment