Go language functions as arguments
Go
language has the flexibility to create functions and use them as arguments to another function. In the following example, we initialize a variable in a defined function just to use the built-in function math.sqrt()
, an example is:
Example
package main
import (
"fmt"
"math"
)
func main(){
/* Declare function variables */
getSquareRoot := func(x float64) float64 {
return math.Sqrt(x)
}
/* Using functions */
fmt.Println(getSquareRoot(9))
}
The result of the above code execution is:
3