Latest web development tutorials

Go language function as a value

Go language function as a value

Go function Go function

Go very flexible language can create a function, and is used as a value. In the following examples we initialize a variable defined function, the function is only to use built-in functions math.sqrt (), examples are:

package main

import (
   "fmt"
   "math"
)

func main(){
   /* 声明函数变量 */
   getSquareRoot := func(x float64) float64 {
      return math.Sqrt(x)
   }

   /* 使用函数 */
   fmt.Println(getSquareRoot(9))

}

The above code is executed as a result of:

3

Go function Go function