Defer, Panic, and Recover
Canonical explanation of `defer`, `panic`, `recover`, and where each belongs in Go code.
Canonical explanation of defer, panic, recover, and where each belongs in Go code.
- Author: Andrew Gerrand
- Authority: primary
- Kind: blog
- URL: https://go.dev/blog/defer-panic-and-recover
What it covers
- defer semantics
- panic usage boundaries
- recover placement and behavior
Why authoritative
Official Go guidance on control-flow primitives that are often misused.
Use when
- when should Go code panic vs return error
- how should defer be used in Go
- when is recover appropriate in Go
Keywords
- defer, panic, recover, cleanup, control flow