Closure in Golang

A closure shares its state across calls.

The following code implements the Fibonacci series from Tour of Go.

https://tour.golang.org/moretypes/26

package mainimport "fmt"// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
first := 0
second := 1
f := func() int {
sum := first + second
first, second = second, sum
return sum
}
return f
}

--

--

Simplify Complexity

Golang, Distributed Systems, File Systems, Python, C/C++, Linux