semikolan;

Til ; ;1 min read

Go's defer runs in LIFO order #

Stacked defers unwind last-in-first-out — which is exactly what you want for paired setup and teardown.

Multiple defer statements in a function run in reverse order — the last one deferred runs first:

func process() {
    f := open()
    defer f.Close()      // runs second

    tx := f.Begin()
    defer tx.Rollback()  // runs first
    // ...
}

This is the right default: teardown happens in the reverse of setup, so inner resources release before the outer ones they depend on. It mirrors how a stack unwinds — which is the whole point.


EOF
filed undergo