0xBF
0xBF

Follow

0xBF

Follow

Go Cheatsheet - Function Closure

0xBF's photo
0xBF
·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