Defer
summary
Go’s defer calls run in LIFO, are useful for closing files and HTTP response bodies, unlocking mutexes, and test cleanup, and appear 418 times in the Pace codebase (418 defers, ~69/month). Common gotcha: loop variable capture makes every deferred closure see the final value unless you pass the loop value into the closure. Under the hood each defer allocates a linked-list node; Go 1.14 open-coded up to 8 defers reduces overhead from ~35ns to ~4ns (naked call ~3ns).
AI-written summary · about
chapters (9)
- 0:00Intro
- 5:34Use Cases
- 14:30Conditions
- 19:13Fact of the Day
- 23:23Defer
- 26:18Defer Performance
- 28:22Open Coding
- 29:40Open Defer
- 33:00Conclusion QA








