Skip to content

Instantly share code, notes, and snippets.

@ascjones
Last active December 28, 2015 18:19
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 ascjones/7542675 to your computer and use it in GitHub Desktop.
Save ascjones/7542675 to your computer and use it in GitHub Desktop.
tour.golang.org solutions
/ ***********************************************************/
/ * Exercise: Fibonacci closure, http://tour.golang.org/#44 */
/ ***********************************************************/
package main
import "fmt"
func fibonacci() func() int {
x, y := 0, 1
return func() int {
x,y = y,x+y
if x == 1 && y == 1 {
return 0
}
return x
}
}
func main() {
f := fibonacci2()
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