Higher-Order Functions

This lesson discusses how to use functions as parameters and values.

Function used as a value

Functions can be used as values just like any other value in Go. In the following code, f1 is assigned a value, the function inc1:

func inc1(x int) int { return x+1 }
f1 := inc1 // f1 := func (x int) int { return x+1 }

Function used as a parameter

Functions can be used as parameters in another function. The passed function can then be called within the body of that function; that is why it is commonly called a callback. To illustrate, here is a simple example:

Get hands-on with 1200+ tech skills courses.