SyntaxPilot
Courses/Go Programming/Lesson 10
Video coming soonThis lesson’s video is being made

Start with the notes below; the video appears here as soon as it is on YouTube.

Go Programming · Lesson 10 of 89

Closures, defer and recursion

anonymous functionsfunction literalsclosures and captured variablesclosure lifetimedeferLIFO order of defersdefer argument evaluation timedefer for cleanuprecursionrecursion depth and growable stacks

These are the notes for Go episode 10. Last episode you learned to write functions, return several results and pass functions around as values. Today: three superpowers of functions. Anonymous functions and closures (a function that remembers values between calls), defer (a cleanup step that runs on every way out of a function, in LIFO order), and recursion (a function that calls itself, with a base case), plus the call stack, stack overflow and Go's growable stacks. Every example runs as-is in the SyntaxPilot compiler (language: Go); outputs and errors are from real runs (Go 1.24).

The problem: a token machine that keeps forgetting

At a clinic, every patient takes a token: 1, 2, 3 and so on. Let's write nextToken() that hands out the next number. The obvious attempt keeps the counter inside the function:

package main

import "fmt"

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

func main() {
    fmt.Println(nextToken(), nextToken(), nextToken())
}
1 1 1

Every patient gets token 1. Why? count is a local variable: a variable declared inside a function. A local variable is created fresh on every call and thrown away when the call ends, so count is born at 0 every time.

Second attempt: move count to package level (outside every function, episode 3), so it survives between calls:

package main

import "fmt"

var count int

func nextToken() int {
    count++
    return count
}

func main() {
    fmt.Println("Dr Rao:", nextToken(), nextToken())
    fmt.Println("Dr Sen:", nextToken())
}
Dr Rao: 1 2
Dr Sen: 3

Dr Rao's tokens are right, but Dr Sen's first patient gets token 3. There's only one count, shared by every caller, and any code in the package can change it. We need each machine to own a private count.

Anonymous functions (function literals)

First, a new tool. Go lets you write a function inside another function, with no name. It's called an anonymous function or function literal: "literal" because you write the function itself in place, the way 5 is an integer literal.

package main

import "fmt"

func main() {
    greet := func(name string) string {
        return "Token for " + name
    }
    fmt.Println(greet("Asha"))
    fmt.Printf("%T\n", greet)

    func() {
        fmt.Println("I run right away")
    }()
}
Token for Asha
func(string) string
I run right away
  • greet := func(...) ... { ... } stores a function value in a variable; you call it like any function.
  • Its type is func(string) string (function types, episode 9).
  • A function literal followed immediately by () is called on the spot. This is sometimes called an "immediately invoked function".
  • You met function literals in episode 8: the tiny func() { fmt.Print(i, " ") } values saved inside a loop.

Closures: functions that remember

Picture a factory that builds token machines. Ask twice, get two machines, and each machine has its own counter sealed inside.

At the clinic In Go Name
The machine factory newCounter() a function that returns a function
Each machine, with its own counter the returned func() int a closure
The sealed counter inside count a captured variable
package main

import "fmt"

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

func main() {
    rao := newCounter()
    sen := newCounter()
    fmt.Println("Dr Rao:", rao(), rao(), rao())
    fmt.Println("Dr Sen:", sen())
    fmt.Println("Dr Rao:", rao())
}
Dr Rao: 1 2 3
Dr Sen: 1
Dr Rao: 4
  • newCounter's result type is func() int: it returns a function.
  • The inner function literal uses count, a variable from the function around it. A function that uses variables from the place where it was created, and keeps them, is called a closure. We say it closes over or captures count.
  • Each call to newCounter() creates a new count, so rao and sen each have their own. Nobody else can touch them: the count is private, unlike the package-level variable.

How long does a captured variable live?

Normally a local variable dies when its function returns. But newCounter has already returned, and count is still counting. Go sees that the closure still uses count, so it keeps count alive as long as the closure exists. This is the closure's lifetime. When nothing can reach the closure any more, Go's garbage collector (the part of the Go runtime that frees memory nobody uses, automatically) cleans it up.

Closure = a function + the variables it captured.

Closures capture variables, not values

A closure doesn't take a snapshot. It shares the variable itself, so it sees later changes:

package main

import "fmt"

func main() {
    discount := 10
    show := func() {
        fmt.Println("discount is", discount)
    }
    show()
    discount = 25
    show()
}
discount is 10
discount is 25

That's why, before Go 1.22, closures created in a loop all saw the loop variable's last value. Since Go 1.22 each iteration has its own variable (episode 8), so each closure captures a different one.

defer: cleanup that can't be skipped

A shopkeeper opens his shutter every morning and must close it before going home. One slow day he leaves early, by a different door, and the shutter stays up all night. Same bug in code:

package main

import "fmt"

func openShop(cash int) {
    fmt.Println("shutter UP")
    if cash == 0 {
        fmt.Println("no customers, going home")
        return
    }
    fmt.Println("sold for", cash)
    fmt.Println("shutter DOWN")
}

func main() {
    openShop(500)
    fmt.Println("---")
    openShop(0)
}
shutter UP
sold for 500
shutter DOWN
---
shutter UP
no customers, going home

The early return skipped the cleanup. Every extra return is one more place to forget.

The fix is defer. defer followed by a function call doesn't run the call now; it schedules it to run just before the surrounding function returns, however it returns (a normal end, any return, or even a panic).

package main

import "fmt"

func openShop(cash int) {
    fmt.Println("shutter UP")
    defer fmt.Println("shutter DOWN")
    if cash == 0 {
        fmt.Println("no customers, going home")
        return
    }
    fmt.Println("sold for", cash)
}

func main() {
    openShop(500)
    fmt.Println("---")
    openShop(0)
}
shutter UP
sold for 500
shutter DOWN
---
shutter UP
no customers, going home
shutter DOWN

Open, then immediately defer the close. The two lines sit together, so you can't forget. This is how Go code closes files and network connections and releases locks:

f, err := os.Open("menu.txt")
if err != nil {
    return err
}
defer f.Close()
// ... read the file; every return below still closes it

(Files and errors get their own episodes: 24 and 34.)

Several defers run in LIFO order

After dinner you stack three plates. The last plate placed on top is the first one washed:

package main

import "fmt"

func main() {
    defer fmt.Println("wash plate 1")
    defer fmt.Println("wash plate 2")
    defer fmt.Println("wash plate 3")
    fmt.Println("dinner is over")
}
dinner is over
wash plate 3
wash plate 2
wash plate 1

Deferred calls run in LIFO order: last in, first out, like a stack of plates. So resources are released in the reverse order you acquired them, which is usually what you want (open A, open B → close B, close A).

defer evaluates its arguments immediately

package main

import "fmt"

func main() {
    price := 100
    defer fmt.Println("defer saw:", price)
    defer func() {
        fmt.Println("closure sees:", price)
    }()
    price = 150
    fmt.Println("now:", price)
}
now: 150
closure sees: 150
defer saw: 100
  • A defer evaluates the function's arguments at the moment the defer line runs, like taking a photo. Only the call itself waits. So defer fmt.Println("defer saw:", price) captured 100.
  • A deferred closure has no arguments; it reads price when it finally runs, so it sees the latest value, 150.
  • And LIFO again: the closure was deferred second, so it runs first.

Forgetting the brackets

package main

import "fmt"

func main() {
    price := 100
    defer func() {
        fmt.Println("closure sees:", price)
    }
    price = 150
}
# command-line-arguments
./main.go:7:11: expression in defer must be function call

Without () it's just a function value, not a call. Write defer func() { ... }().

Recursion: a function that calls itself

A long queue at a ration shop. How many people are ahead of you? You can't see the front, so you ask the person ahead of you. They don't know either, so they ask the person ahead of them: the same question, for a slightly shorter queue. The very first person says "nobody is ahead of me: zero". Then the answers flow back, each person adding one.

A function that calls itself is recursive, and the technique is recursion: solving a problem using a smaller copy of the same problem. Every recursive function needs:

  1. A base case: the smallest problem, answered directly, with no further calls (the first person in the queue).
  2. A recursive case: a call to itself on a smaller input, building the answer from its result.

Factorial is the classic example. 5! ("5 factorial") = 5 × 4 × 3 × 2 × 1 = 120: the number of ways to seat 5 friends on a bench. Notice 5! = 5 × 4!, and by definition 0! = 1.

package main

import "fmt"

func fact(n int) int {
    if n == 0 {
        return 1
    }
    return n * fact(n-1)
}

func main() {
    fmt.Println(fact(3))
    fmt.Println(fact(5))
    fmt.Println(fact(10))
}
6
120
3628800

How fact(3) runs:

fact(3) = 3 * fact(2)
              fact(2) = 2 * fact(1)
                            fact(1) = 1 * fact(0)
                                          fact(0) = 1        <- base case
                            fact(1) = 1 * 1 = 1
              fact(2) = 2 * 1 = 2
fact(3) = 3 * 2 = 6

Forgetting the base case: stack overflow

package main

import "fmt"

func fact(n int) int {
    return n * fact(n-1)
}

func main() {
    fmt.Println(fact(5))
}
runtime: goroutine stack exceeds 1000000000-byte limit
runtime: sp=0xc0200e0398 stack=[0xc0200e0000, 0xc0400e0000]
fatal error: stack overflow

runtime stack:
runtime.throw({0x4b5337?, 0x410b51?})
...

Nobody ever says "zero", so the calls never stop.

The call stack

Every call that hasn't finished is waiting for an answer. For each one, Go keeps a stack frame: a small block of memory holding that call's parameters and local variables. Frames pile up on the call stack (a stack, like the plates: the newest call is on top and finishes first):

| fact(3) ...  |  <- running
| fact(4)      |  waiting
| fact(5)      |  waiting
| main         |  waiting

Without a base case the stack grows until it hits its limit: a stack overflow. Go's documented default limit is 1 GB per goroutine stack on 64-bit systems (250 MB on 32-bit), which is the 1000000000-byte limit in the message. (A goroutine is a function running independently in Go; main runs in one. Goroutines get their own episode, 43.)

Go's growable stacks

In many languages a thread's stack has a fixed size of a few megabytes, so deep recursion crashes early. In Go, a goroutine's stack starts small, 2048 bytes (2 KB) since Go 1.4, and grows automatically: when it runs out, Go allocates a bigger stack and copies the frames over. So this runs fine:

package main

import "fmt"

func sum(n int) int {
    if n == 0 {
        return 0
    }
    return n + sum(n-1)
}

func main() {
    fmt.Println(sum(1000000))
}
500000500000

One million calls deep. Still, a loop is usually faster and uses less memory. Use recursion when the problem is naturally nested: folders inside folders, replies inside comments, trees (later in the course).

Mini project: a call tracer

Recursion plus defer lets you see the call stack grow and shrink:

package main

import "fmt"

func climb(step int) {
    if step == 0 {
        fmt.Println("  reached the top!")
        return
    }
    fmt.Println("enter", step)
    defer fmt.Println("exit", step)
    climb(step - 1)
}

func main() {
    climb(3)
}
enter 3
enter 2
enter 1
  reached the top!
exit 1
exit 2
exit 3

Each call's defer runs when that call returns, and the deepest call returns first.

Extension: add a closure that counts the steps:

package main

import "fmt"

func main() {
    steps := 0
    var climb func(n int)
    climb = func(n int) {
        if n == 0 {
            return
        }
        steps++
        climb(n - 1)
    }
    climb(5)
    fmt.Println("steps climbed:", steps)
}
steps climbed: 5

Note var climb func(n int) first: a function literal can only call itself recursively if the variable already exists when the literal is written.

Common mistakes (and the real error messages)

You wrote What happens Fix
count := 0 inside nextToken no error: prints 1 1 1 capture count in a closure
a package-level counter for two users no error: they share one count one closure per user
defer func() { ... } (no ()) expression in defer must be function call defer func() { ... }()
defer fmt.Println(price), then change price prints the old value defer a closure to read the latest value
cleanup as the last line, plus an early return cleanup skipped on the early path defer the cleanup right after acquiring
recursion with no base case fatal error: stack overflow if n == 0 { return 1 }
a base case that is never reached, e.g. fact(-1) fatal error: stack overflow guard bad input: if n <= 0

Practice

  1. Write newCounter(start, step int) func() int so c := newCounter(10, 5) gives 15, 20, 25.
  2. Defer three prints and predict the order before running.
  3. Write a recursive power(base, exp int) int.
  4. Write a recursive digitSum(n int) int: digitSum(4096) is 19.
Solutions

1.

package main

import "fmt"

func newCounter(start, step int) func() int {
    n := start
    return func() int {
        n += step
        return n
    }
}

func main() {
    c := newCounter(10, 5)
    fmt.Println(c(), c(), c())
}
15 20 25
  1. They print in reverse: last deferred, first printed.

3.

package main

import "fmt"

func power(base, exp int) int {
    if exp == 0 {
        return 1
    }
    return base * power(base, exp-1)
}

func main() {
    fmt.Println(power(2, 10), power(3, 4))
}
1024 81
  1. The last digit is n % 10, the rest is n / 10:
package main

import "fmt"

func digitSum(n int) int {
    if n < 10 {
        return n
    }
    return n%10 + digitSum(n/10)
}

func main() {
    fmt.Println(digitSum(4096))
}
19

Quick quiz

What does this print?

func main() {
    for i := 1; i <= 3; i++ {
        defer fmt.Print(i, " ")
    }
    fmt.Print("go ")
}
  • A. go 1 2 3
  • B. 1 2 3 go
  • C. go 3 2 1
  • D. 3 2 1 go
Answer

C. go 3 2 1. Deferred calls wait until main is about to return, so go prints first. Each defer captured i's value when its line ran (arguments are evaluated immediately), and they run last in, first out.

Interview questions

  • What is a closure? What does it capture, a value or a variable?
  • What happens to a local variable captured by a closure after its function returns?
  • What does defer do? When does a deferred call run?
  • In what order do multiple deferred calls run? Why is that useful?
  • When are the arguments of a deferred call evaluated? How do you defer something that sees a later value?
  • What two parts must every recursive function have?
  • What is a stack frame? What is a stack overflow?
  • How do Go's goroutine stacks differ from fixed-size thread stacks? What is the default maximum stack size?
  • When would you choose recursion over a loop?

Glossary

Term Meaning Example
Local variable a variable declared inside a function; created fresh on each call count := 0 in nextToken
Package-level variable declared outside all functions; shared by the whole package var count int
Anonymous function a function with no name func() { ... }
Function literal a function written in place as a value greet := func(name string) string { ... }
Immediately invoked function a function literal called right where it's written func() { ... }()
Closure a function together with the variables it captured the function returned by newCounter
Capture / close over a closure keeping a variable from its surrounding function count inside the returned func
Captured variable a variable used by a closure count
Closure lifetime a captured variable lives as long as a closure uses it count outlives newCounter
Garbage collector the part of Go that frees unused memory automatically frees a closure nobody uses
defer schedules a call to run just before the surrounding function returns defer fmt.Println("shutter DOWN")
Deferred call a call scheduled with defer f.Close() in defer f.Close()
Cleanup work that must happen when you're done (close, unlock) closing a file
LIFO last in, first out: the newest item comes out first three defers print 3, 2, 1
Stack (data structure) a pile where you add and remove only at the top plates
Argument evaluation time defer computes arguments when the defer line runs defer saw: 100
Recursion solving a problem with a smaller copy of the same problem fact(n-1) inside fact
Recursive function a function that calls itself fact
Base case the smallest case, answered directly with no more calls if n == 0 { return 1 }
Recursive case the case that calls itself on a smaller input return n * fact(n-1)
Factorial (n!) 1 × 2 × ... × n; 0! = 1 5! = 120
Call stack the stack of function calls that haven't finished yet main → fact(5) → fact(4) ...
Stack frame one call's block of memory: its parameters and locals the frame for fact(3)
Stack overflow the call stack runs out of room fatal error: stack overflow
Recursion depth how many calls deep the recursion goes sum(1000000) is a million deep
Goroutine a function running independently in Go; main runs in one (episode 43) go f()
Growable stack a goroutine stack that starts at 2 KB and grows by copying deep recursion works
Panic Go stopping a program because of a run-time error nil pointer dereference

Summary

  • Local variables are created fresh on every call, so they can't remember anything between calls.
  • A function literal is a function with no name, written in place; add () to call it right away.
  • A closure captures variables from where it was created and keeps them alive. Each call to a factory like newCounter creates fresh, private variables.
  • defer runs a call just before the function returns, on every path. Defers run LIFO, and their arguments are evaluated immediately; a deferred closure reads values at the end.
  • Recursion solves a problem with a smaller copy of itself. It needs a base case, or the call stack overflows. Go's stacks start at 2 KB and grow, up to 1 GB on 64-bit systems.

Next episode: pointers. Memory addresses, & and *, nil, new, and finally a function that changes your variable.