At the end of the Day 1 Go Course slides (pdf) there is an exercise stated as follows (NOTE: the course in the linked presentation is considered obsolete. If you are looking to learn Go the suggested route is first via http://tour.golang.org):
You all know what the Fibonacci series is. Write a package to implement it. There should be a function to get the next value. (You don't have structs yet; can you find a way to save state without globals?) But instead of addition, make the operation settable by a function provided by the user. Integers? Floats? Strings? Up to you.
How does the following solution rate with respect to its 'Go'-iness?
package fib
type BinOp func (uint64, uint64) uint64
func fib_intern(a, b uint64, op BinOp) uint64 {
Fib = func(opnew BinOp) uint64 {
return fib_intern(b, op(a,b), opnew)
}
return a
}
var Fib = func(op BinOp) uint64 {
return fib_intern(0,1,op)
}
Which can then be called like:
package main
import (
"./fib"
"fmt"
)
func add(a, b uint64) uint64 {
return a+b
}
func main() {
for i := 0; i < 20; i++ {
fmt.Println(fib.Fib(add))
}
}