WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Go - functions as values


The Go language can be flexible to create functions and act as arguments to another function. In the following example we initialize a variable in the defined function, just to use the built-in function math.sqrt(), the 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 above code execution results are:

3