Go Cheatsheet - Function Closure
Functions
Closure to hide variable
// i will be saved in this function's context
func intSeq() func() int {
i := 0
return func() int {
i++
return i
}
}
nextInt := intSeq()
nextInt() // 1
nextInt() // 2