Go language functions as arguments


Release date:2023-09-12 Update date:2023-10-13 Editor:admin View counts:296

Label:

Go language functions as arguments

Go language has the flexibility to create functions and use them as arguments to another function. In the following example, we initialize a variable in a defined function just to use the built-in function math.sqrt() , an example is:

Example

package main
import (
   "fmt"
   "math"
)
func main(){
   /* Declare function variables */
   getSquareRoot := func(x float64) float64 {
      return math.Sqrt(x)
   }
   /* Using functions */
   fmt.Println(getSquareRoot(9))
}

The result of the above code execution is:

3

Powered by TorCMS (https://github.com/bukun/TorCMS).