Rob Pike's Golang presentation excercise of day 1 I'm following this presentation. At the very end there's an exercise about solving Fibonacci and says that instead of addition, make the operation setteable by a function.

Is the following is a good solution? How close might it be to what Rob Pike would have written?

package main

import "fmt"

func add(a, b int) int {
    return a + b
}

func subtract(a, b int) int {
    return a - b
}

func power(a, b int) int {
    return a ^ b
}

// fib returns a function that returns
// successive Fibonacci numbers.
func fib(op func(int, int) int) func() int {
    n0, n1 := 0, 1
    return func() int {
        n0, n1 = n1, op(n0, n1)
        return n0
    }
}

func main() {
    f := fib(add)
    // Function calls are evaluated left-to-right.
    fmt.Println(f(), f(), f(), f(), f(), f(), f())

    f = fib(subtract)
    // Function calls are evaluated left-to-right.
    fmt.Println(f(), f(), f(), f(), f(), f(), f())

    f = fib(power)
    // Function calls are evaluated left-to-right.
    fmt.Println(f(), f(), f(), f(), f(), f(), f())
}
This seems like a good solution except for one thing:

func power(a, b int) int {
    return a ^ b
}


The ^ operator doesn't do what you think it does. It's a bitwise xor operator. There is no power operator in Go, there's only math.Pow for float64s.

On a side note, I personally would create a type for operators:

type Op func(int, int) int


It's easier to type in function definitions and allows you to extend it with methods like String().