Skip to content

Instantly share code, notes, and snippets.

@patrickbrandt
Created December 16, 2018 20:34
Show Gist options
  • Save patrickbrandt/657e87b916477c129e4b652d98ba8a81 to your computer and use it in GitHub Desktop.
Save patrickbrandt/657e87b916477c129e4b652d98ba8a81 to your computer and use it in GitHub Desktop.
Fibonacci in Go
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
// this is my solve to https://tour.golang.org/moretypes/26
func fibonacci() func() int {
next := -1
sum := 0
current := 0
return func() int {
if (next < 1 ) {
next = next + 1
return next
}
sum = current + next
current = next
next = sum
return sum
}
}
func main() {
f := fibonacci()
for i := 0; i < 10; i++ {
fmt.Println(f())
}
}
@patrickbrandt
Copy link
Author

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