ProgramGuru

Closures

Start with Go Closures

A closure is a function value that refers to variables defined outside its own body. It can continue using those captured variables across calls.

What you’ll learn

  • Explain the main purpose of go closures.
  • Read and run the complete Go example in this lesson.
  • Apply the concept and recognize common mistakes.

Understand the idea

Go function literals can access surrounding local variables. When a function escapes the scope where it was created, the language keeps the captured state available for as long as needed.

A counter closure is an approachable example: each call changes the same captured count. Separate calls to the factory create separate counts. Closures are useful for encapsulating state, but shared mutable state needs care when accessed concurrently.

Visualize how it works

Follow this diagram before executing the example. Check where a value or decision changes what runs next.

Visual explanation for closuresThe same captured variable is updated across calls.newCounter()Step 1Capture count=0Step 2Call -> count=1Step 3Call -> count=2Step 4The same captured variable is updated across calls.
The same captured variable is updated across calls.

Run a complete Go example

This example is a complete program. Save it as main.go or paste it into the Go Playground.

Go code

package main

import "fmt"

func newCounter() func() int {
	count := 0
	return func() int { count++; return count }
}
func main() {
	first := newCounter()
	second := newCounter()
	fmt.Println(first(), first(), second(), first())
}

Output

1 2 1 3

Trace what the program does

The two calls to newCounter() create different captured count variables. first() yields 1, then 2; second() starts at 1; finally first() yields 3. The count persists between invocations.

Avoid common mistakes

  • Captured variables are not automatically shared between separate factory calls.
  • A closure does not make shared state thread-safe; concurrent mutation may still require synchronization.

Try it yourself

Change the counter to increase by 2 each call, then create two independent counters and predict their output.

Tip: Edit the example above, predict the output, and run the modified code to test your reasoning.

Check your understanding

Why does first() continue from its previous value?
Choose one answer. You can try again.

Answer and explanation

It captures a persistent count variable

Correct. The two calls to newCounter() create different captured count variables.

What you should remember

You have practiced Go Closures in a small, self-contained program. The important habit is to predict what runs, inspect the actual output and distinguish the language rule from the particular example.

Before continuing, make sure you can explain the program to someone who has not seen it, including the line that changes its result.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Recursive Functions →