Go Closures will be explained in this post. First we’ll start of with an introduction: functions can contain functions. Then we’ll give an example of a closure and finally a practical app.
Closure
Nested functions
Go functions can contain functions. Those functions can literally be defined in functions.
The example below outputs “hello world”, but demonstrates this nested functions principle.
func main(){
:= func() string {
world return "world"
}
.Print("hello ", world(),"\n")
fmt}
What is a closure?
In the next example, it creates a closure. The closure returns a number that increases each time its called.
You can use the variable x inside the increment function below. The
function increment
and variable x
form the
closure.
func main(){
:= 2
x := func() int {
increment ++
xreturn x
}
.Println(increment())
fmt.Println(increment())
fmt.Println(increment())
fmt}
x will keep being modified by the increment
function.
Each time the function increment()
is called,
x
is modified.
{% pullquote %} A closure is a type of function, that uses variables defined outside of the function itself. {% endpullquote %}
Generator
You can use the idea of closures to make a number generator.
In the example below function makeSequence()
returns an
anonymous function that generates odd numbers. An anonymous function is
just a function without a name.
func makeSequence() func() int {
:=1
ireturn func() int {
+=2
ireturn i
}
}
makeSequence()
returns a function, that function returns
the numeric output.
Then we create a function: sequence generator:
:= makeSequence() sequenceGenerator
And use it like this:
.Println(sequenceGenerator())
fmt.Println(sequenceGenerator()) fmt
Full code:
package main
import "fmt"
func makeSequence() func() int {
:=1
ireturn func() int {
+=2
ireturn i
}
}
func main(){
:= makeSequence()
sequenceGenerator .Println(sequenceGenerator())
fmt.Println(sequenceGenerator())
fmt.Println(sequenceGenerator())
fmt}