Closures in Go
Golang Concepts
Go: Closures
Go supports anonymous functions, which can form closures. Anonymous functions are useful when you want to define a function inline without having to name it.
Closure example in Go
package main
import "fmt"
// this will form closure
func outerFunction() func() int {
x := 10
return func() int {
x++
return x
}
}
func main() {
increment := outerFunction()
fmt.Println(increment())
fmt.Println(increment())
}
// 11
// 12