Go 语言函数作为值
Go 语言教程
· 2019-02-13 07:29:48
Go 语言可以很灵活的创建函数,并作为值使用。以下实例中我们在定义的函数中初始化一个变量,该函数仅仅是为了使用内置函数 math.sqrt() ,实例为:
package main import ( "fmt" "math" ) func main(){ /* 声明函数变量 */ getSquareRoot := func(x float64) float64 { return math.Sqrt(x) } /* 使用函数 */ fmt.Println(getSquareRoot(9)) }
以上代码执行结果为:
3
点击查看所有 Go 语言教程 文章: https://www.codercto.com/courses/l/13.html