Easy Tutorial
❮ Go For Loop Go If Else Statement ❯

Go Language Functions as Arguments

Go Functions

Go language allows for the flexible creation of functions and their use as arguments in another function. In the following example, we initialize a variable within a defined function, which is solely for utilizing the built-in function math.Sqrt(). The example is as follows:

Example

package main

import (
   "fmt"
   "math"
)

func main(){
   /* Declare function variable */
   getSquareRoot := func(x float64) float64 {
      return math.Sqrt(x)
   }

   /* Use the function */
   fmt.Println(getSquareRoot(9))

}

The above code execution result is:

3

Go Functions

❮ Go For Loop Go If Else Statement ❯