Skip to content

Instantly share code, notes, and snippets.

@hunterloftis
Created June 4, 2017 04:51
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 hunterloftis/58a061913a5d3611aaa61580bf9c45f3 to your computer and use it in GitHub Desktop.
Save hunterloftis/58a061913a5d3611aaa61580bf9c45f3 to your computer and use it in GitHub Desktop.
learning go
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
prev := [2]int{0, 1}
return func() int {
n := prev[0]
next := prev[0] + prev[1]
prev = [2]int{prev[1], next}
return n
}
}
func main() {
f := fibonacci()
for i := 0; i < 10; i++ {
fmt.Println(f())
}
}
@sch
Copy link

sch commented Jun 4, 2017

Why the memory/syntactic overhead of a list for prev? Why not two variables?

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