Go Cheatsheet - Function Closure

·

1 min read

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