Function Values
In Go, a function is a value, just like a number or a string. You can store it in a variable, and you can pass it to another function.
package main
import "fmt"
func double(n int) int {
return n * 2
}
func square(n int) int {
return n * n
}
func main() {
f := double
fmt.Println(f(5))
f = square
fmt.Println(f(5))
}
Output:
10
25
Look closely at f := double. There are no parentheses after double. With parentheses, double(5) calls the function. Without them, double is the function itself, and f := double stores it in f. After that, f(5) calls whatever function is in f. The first time that is double, and after f = square it is square.
Function types
A function value has a type, like every value in Go. The type lists the parameter types and the return type. double and square both take one int and return one int, so both have the type func(int) int.
You can use this type to declare a variable. A variable of type func(int) int can hold only functions of that shape:
package main
import "fmt"
func double(n int) int {
return n * 2
}
func greet(name string) string {
return "Hello, " + name
}
func main() {
var f func(int) int
f = double
fmt.Println(f(4))
f = greet
}
Output:
# command-line-arguments
./main.go:17:6: cannot use greet (value of type func(name string) string) as func(int) int value in assignment
var f func(int) int declares f as a variable that holds a function with one int in and one int out. greet takes a string and returns a string, so it does not fit in f. Because of that one line, the whole program does not compile, and nothing is printed.
Passing a function to a function
A parameter can have a function type too. Then the caller decides what the function does:
package main
import "fmt"
func double(n int) int {
return n * 2
}
func square(n int) int {
return n * n
}
func showTable(f func(int) int) {
for i := 1; i <= 3; i++ {
fmt.Println(i, "->", f(i))
}
}
func main() {
showTable(double)
fmt.Println("---")
showTable(square)
}
Output:
1 -> 2
2 -> 4
3 -> 6
---
1 -> 1
2 -> 4
3 -> 9
showTable has one loop, and it works with any func(int) int. Without function values, you would need one loop for doubling and another for squaring.
Anonymous functions
Sometimes you need a small function in one place only. You can write it without a name, right where you need it. This is called an anonymous function, or a function literal. It looks like a normal function with no name after func. You can store it in a variable:
addOne := func(n int) int {
return n + 1
}
fmt.Println(addOne(7))
Output:
8
addOne now holds a func(int) int, so you could also pass it to showTable. You can also put a function literal straight into a call, as in showTable(func(n int) int { return n * 10 }).
A function literal can even be called the moment you write it. The () after the closing brace is the call:
func() {
fmt.Println("runs right away")
}()
Output:
runs right away
This last form looks strange at first. You will use it with defer in the Defer lesson.
In the exercise, complete applyTwice, which calls a function two times, and store an anonymous function in addTen.