100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Go

Closures in Go

Explore how Go closures capture and reference variables from their enclosing scope.

FunctionsIntermediate8 min readJul 8, 2026
Analogies

Introduction

A closure is a function value that references variables from outside its own body. Because functions in Go are first-class values, a function literal can be defined inline and returned from another function, carrying a reference to the variables it captured from its enclosing scope. This allows the closure to read and modify that state across multiple calls.

🏏

Cricket analogy: A commentator who has watched every over remembers the exact match situation and references it in every sentence, just as a closure carries a live reference to the variables from its enclosing scope, not just a copy from one moment.

Syntax

go
func makeCounter() func() int {
    count := 0
    return func() int {
        count++
        return count
    }
}

Explanation

In makeCounter, the variable count is declared inside the outer function. The anonymous function returned by makeCounter captures count by reference, not by copying its value. Each time the returned function is called, it increments and returns the same count variable, so the state persists between calls. Every call to makeCounter() creates a brand-new, independent count variable, so separate closures do not share state unless they explicitly close over the same variable.

🏏

Cricket analogy: The count variable inside makeCounter is like a personal wicket tally kept by a specific bowler; each delivery updates that same tally, but a different bowler brought in via a fresh spell (a new makeCounter() call) starts their own tally at zero, unrelated to the first.

Example

go
package main

import "fmt"

func makeCounter() func() int {
    count := 0
    return func() int {
        count++
        return count
    }
}

func main() {
    counterA := makeCounter()
    counterB := makeCounter()

    fmt.Println(counterA())
    fmt.Println(counterA())
    fmt.Println(counterA())
    fmt.Println(counterB())
}

// Output:
// 1
// 2
// 3
// 1

Key Takeaways

  • A closure is a function value that captures variables from its enclosing scope.
  • Captured variables are referenced, not copied, so state persists across calls.
  • Each invocation of the outer function creates a fresh, independent set of captured variables.
  • Closures are commonly used to build counters, generators, and stateful callbacks.
  • Since functions are first-class values, closures can be returned, stored, and passed around freely.

Practice what you learned

Was this page helpful?

Topics covered

#Go#GoProgrammingStudyNotes#Programming#ClosuresInGo#Closures#Syntax#Explanation#Example#Functions#StudyNotes#SkillVeris