0xBF
0xBF

0xBF

Go Cheatsheet - Function Closure

0xBF's photo
0xBF

Published on Apr 22, 2021

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
 
Share this