Skip to content

Instantly share code, notes, and snippets.

@gylaz
Last active December 15, 2015 08:59
Show Gist options
  • Save gylaz/5235308 to your computer and use it in GitHub Desktop.
Save gylaz/5235308 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.
func fibonacci() func() int {
current_fib_number := 0
prev_fib_number := 0
f := func() int {
result := prev_fib_number + current_fib_number
prev_fib_number = current_fib_number
if result == 0 {
current_fib_number += 1
} else {
current_fib_number = result
}
return result
}
return f
}
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