A closure in Go is a function value that captures variables from the scope where it was created and keeps using them after that scope has returned. Anonymous functions (function literals) are the usual way to create closures: you write func(...) { ... } inline, and it can read and modify the variables around it.
Closures let you attach private state to a function without defining a struct. They are common in counters, middleware, option functions, callbacks and goroutine bodies.
An anonymous function is a function literal with no name. You can assign it to a variable, pass it as an argument, return it, or call it immediately.
A closure is an anonymous function that refers to variables declared outside its own body. The compiler keeps those variables alive (they escape to the heap if needed) for as long as the function value is reachable.
The classic closure example is a counter generator. Each call to newCounter creates a new count variable, and the returned function is the only code that can touch it.
a and b do not share state because each call to newCounter allocates its own count. The variable is captured by reference, not copied, so every call to the same closure sees the updated value.
Because capture is by reference, changing the outer variable after creating the closure changes what the closure sees.
If you want a snapshot, pass the value as an argument or copy it into a new variable.
Before Go 1.22 a for loop reused one variable for every iteration, so closures created in the loop all saw the final value. Since Go 1.22 each iteration has its own copy of the loop variable, so this code now behaves as most people expect:
The output order is not deterministic, but it prints 0, 1 and 2 once each. The per-iteration behaviour depends on the go version in your go.mod; with an older language version you must pass i as a parameter (go func(i int) { ... }(i)).
| Use case | How a closure helps |
|---|---|
| Counters and generators | Keep state between calls without a struct |
| HTTP middleware | Wrap a handler and capture configuration |
| Functional options | Return func(*Server) values that modify a config |
| Callbacks and sorting | sort.Slice and slices.SortFunc take a comparison closure |
| Deferred cleanup | defer func() { ... }() can read and set named results |
The returned handler closes over next, so each wrapped handler remembers the handler it delegates to.
sync.Mutex or an atomic type such as atomic.Int64. The race detector (go test -race) finds these bugs.No. An anonymous function is a function without a name. It becomes a closure when it uses variables from an enclosing scope. A named function value can also be a closure if it was created inside another function.
No. Go captures variables by reference, so the closure and the enclosing code share the same variable. Copy it into a new variable first if you need a snapshot.
Calling a closure through a function value costs an indirect call, and captured variables may be heap allocated. For most code the difference is negligible; measure with go test -bench before optimising.
Yes, but you must declare the variable first: var walk func(n int) and then assign walk = func(n int) { ... walk(n-1) }.
Anonymous functions are function literals, and closures are those literals that capture surrounding variables by reference. Use them for private state, middleware, options and callbacks. Remember that Go 1.22 gives each loop iteration its own variable, protect shared state with a mutex or atomics, and prefer a named type when the logic grows beyond a few lines.