Go Function As Values
# Go Functions as Arguments
[Go Functions](#)
In Go, functions are first-class citizens, meaning they can be assigned to variables, passed as arguments, and used just like ordinary variables. Passing a function as an argument to another function is a common programming pattern in Go, often used for callbacks, strategy patterns, and similar scenarios.
* * *
## Assigning a Function to a Variable
A function can be assigned to a variable, and the function can be 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 above code is:
34
* * *
## Passing a Function as an Argument
When passing a function as an argument to another function, the receiving function needs to declare a corresponding function type parameter. The following example defines an `apply` function that accepts an operation function and two operands, performing any specified operation on the two numbers:
## Example
package main
import(
"fmt"
"math"
)
/* Define a function that accepts a function-type parameter op */
/* The type of op is func(float64, float64) float64, representing a function that accepts 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 above code is:
7121024
* * *
## Practical Application: Custom Sorting Rules
One of the most common applications of passing functions as arguments is customizing sorting rules. The `sort.Slice` function in Go's standard library accepts a comparison function as a parameter:
## Example
package main
import(
"fmt"
"sort"
)
func main(){
nums :=[]int{5,2,8,1,9,3}
/* Pass the comparison function as an argument to sort.Slice to sort in ascending order */
sort.Slice(nums,func(i, j int)bool{
return nums nums
})
fmt.Println("Descending:", nums)// Output: Descending:
}
The execution result of the above code is:
Ascending: Descending:
By passing different functions as arguments, the same `sort.Slice` can implement completely different sorting logic. This is the core value of passing functions as argumentsβ**parameterizing behavior to make code more flexible and reusable**.
[Go Functions](#)
YouTip