Go 函数作为值 Go编程语言提供灵活性,可以动态创建函数并将其用作值。在下面的示例中,我们初始化了一个变量,并给它赋予了一个函数定义。这个函数变量的目的是用于内置的math.sqrt()函数。例如: package main import ("fmt" "math") func main(){ /* declare a function variable */ getSquareRoot := func(x float64) float64 { return math.Sqrt(x) } /* use the function */ fmt.Println(getSquareRoot(9)) }GoCopy 当上述代码被编译和执行时,会产生以下结果− 3GoCopy