Skip to main content

Go Cheatsheet - Function Closure

Updated 1 min readMarkdown
Part of seriesGo Cheatsheet

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

No comments yet. Be the first to comment.