Go Functions as Arguments
In Go, functions are first-class citizens and can be assigned, passed, and used just like ordinary variables. Passing a function as an argument to another function is a common programming pattern in Go, often used in scenarios such as callbacks and the strategy pattern.
Assigning a function to a variable
A function can be assigned to a variable, and called through that variable:
Example
package
main
import
(
"fmt"
"math"
)
func
main
()
{
/* Assign an anonymous function to the variable getSquareRoot */
getSquareRoot
:=
func
(
x
float64
)
float64
{
return
math
.
Sqrt
(
x
)
}
/* Call the function through the variable */
fmt
.
Println
(
getSquareRoot
(
9
))
// Output: 3
fmt
.
Println
(
getSquareRoot
(
16
))
// Output: 4
}
The execution result of the code above is:
3
4
Passing a function as an argument
When passing a function as an argument to another function, the receiver needs to declare a corresponding function-type parameter. The following example defines an apply function that receives an operation function and two operands, performing any specified operation on the two numbers:
Example
package
main
import
(
"fmt"
"math"
)
/* Define a function that receives a function-type parameter op */
/* The type of op is func(float64, float64) float64, representing a function that takes two float64 parameters and returns a float64 */
func
apply
(
op
func
(
float64
,
float64
)
float64
,
a
,
b
float64
)
float64
{
return
op
(
a
,
b
)
}
func
main
()
{
/* Define an addition function */
add
:=
func
(
a
,
b
float64
)
float64
{
return
a
b
}
/* Define a multiplication function */
multiply
:=
func
(
a
,
b
float64
)
float64
{
return
a
b
}
/* Pass the add function as an argument to apply */
fmt
.
Println
(
apply
(
add
,
3
,
4
))
// Output: 7
/* Pass the multiply function as an argument to apply */
fmt
.
Println
(
apply
(
multiply
,
3
,
4
))
// Output: 12
/* You can also pass an anonymous function directly */
fmt
.
Println
(
apply
(
func
(
a
,
b
float64
)
float64
{
return
math
.
Pow
(
a
,
b
)
// a to the power of b
},
2
,
10
))
// Output: 1024
}
The execution result of the code above is:
7
12
1024
Practical application: custom sorting rules
One of the most common applications of functions as arguments is custom sorting rules. The Go standard library's sort.Slice accepts a comparison function as an argument:
Example
package
main
import
(
"fmt"
"sort"
)
func
main
()
{
nums
:=
[]
int
{
5
,
2
,
8
,
1
,
9
,
3
}
/* Pass a comparison function as an argument to sort.Slice to sort from smallest to largest */
sort
.
Slice
(
nums
,
func
(
i
,
j
int
)
bool
{
return
nums
[
i
]
< nums
[
j
]
})
fmt
.
Println
(
"Ascending:"
,
nums
)
// Output: Ascending: [1 2 3 5 8 9]
/* Pass a different comparison function to sort from largest to smallest */
sort
.
Slice
(
nums
,
func
(
i
,
j
int
)
bool
{
return
nums
[
i
]
nums
[
j
]
})
fmt
.
Println
(
"Descending:"
,
nums
)
// Output: Descending: [9 8 5 3 2 1]
}
The execution result of the code above is:
升序:[1 2 3 5 8 9]
降序:[9 8 5 3 2 1]
By passing different functions as arguments, the same sort.Slice can implement completely different sorting logic; this is exactly the core value of functions as arguments—parameterizing behavior to make code more flexible and reusable。
Go Functions