Skip to content

Instantly share code, notes, and snippets.

@parasyte
Created November 15, 2012 09:44
Show Gist options
  • Save parasyte/4077694 to your computer and use it in GitHub Desktop.
Save parasyte/4077694 to your computer and use it in GitHub Desktop.
Fibonacci closure: http://tour.golang.org/#48
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
x, y := 0, 1
return func() int {
x, y = y, x + y
return x
}
}
func main() {
f := fibonacci()
for i := 0; i < 10; i++ {
fmt.Println(f())
}
}
@sina-devel
Copy link

func Fibonacci() func() uint {
	var a, b uint = 1, 0
	return func() uint {
		a, b = b, a+b
		return a
	}
}

@msh2050
Copy link

msh2050 commented Jan 27, 2022

thanks for the gist,it is very much useful
I make Benchmarking for deferent Fibonacci functions and algorithms with running unit test
https://github.com/msh2050/GoFibonacciBench

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment