当前位置: 首页 > 文档资料 > GO 语言基础教程 >

Go 语言函数作为值

优质
小牛编辑
135浏览
2023-12-01

Go 函数

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 函数