More Functions
Function Values
Functions are values too. They can be passed around just like other values. Function values may be used as function arguments and return values.
Example
Function Closures
Go functions may be closures. A closure is a function value that references variables from outside its body. The function may access and assign to the referenced variables; in this sense the function is "bound" to the variables.
Example 1 (No Parameter)
Notice here a
and
have their own closure and each is bound to its own bx
variable
Example 2 (With Parameter)
The adder
function returns a closure. Each closure is bound to its own sum
variable.
More About Closure
Last updated