public
Authored by avatar PotatoGim

[Go] Practice: Fibonacci Closure

Edited
gotour_practice_fib_closure.go 372 bytes
package main

import "fmt"

// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
    x, y, fib := 0, 1, 0
    
    return func() int {
        x, y, fib = y, fib, y+fib
        return fib
    }
}

func main() {
    f := fibonacci()
    
    for i := 0; i < 10; i++ {
        fmt.Println(f())
    }
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment