Deferred calls stack; the last one you register runs first when the function returns.
In Go, defer schedules a function call to run when the surrounding function returns. Stack a few of them and the order surprises almost everyone the first time.
Deferred calls run last-in, first-out. The most recently deferred call runs first. Picture a stack of plates: you add to the top, and you take from the top.
Gofunc process() { defer fmt.Println("third") defer fmt.Println("second") defer fmt.Println("first") // prints: first, second, third }
RULE OF THUMB Read your defers from the bottom up. The last one you write is the first to run.
That ordering is exactly what you want for cleanup: open a file, defer its close; grab a lock, defer its unlock. Resources are released in the reverse order they were acquired — which is almost always the correct order.