Go handles failures with returned error values and recovers from them with a small set of techniques: checking and falling back, retrying with backoff, cleaning up with defer, converting panics into errors with recover, and cancelling work with context. Which one fits depends on whether the failure is expected, transient or a bug.
This article walks through those techniques by scenario, so you can pick the right recovery strategy instead of applying if err != nil { return err } everywhere.
| Scenario | Typical failure | Recovery technique |
|---|---|---|
| Optional resource missing | os.ErrNotExist | check with errors.Is, use a default |
| Network or database blip | timeout, connection reset | retry with backoff and a limit |
| Bad user input | validation error | return a typed error, report to the user |
| Bug in one job or request | nil dereference, index out of range | recover at the boundary, log, continue |
| Caller gave up | cancellation or deadline | stop work, return ctx.Err() |
| Several independent failures | batch processing | collect with errors.Join |
Inspect the error and decide. Use errors.Is for sentinel values and errors.As for typed errors.
Only swallow errors you have explicitly identified. Every other error is passed up with context.
Retry only operations that are safe to repeat (idempotent), cap the attempts, and stop when the context ends.
In production add random jitter to the delay so many clients do not retry in lockstep.
defer runs when the function returns or panics, which makes it the place to release files, locks and connections. Check the error from Close on files you wrote to.
The named result err lets the deferred function change what the caller receives.
A panic in a goroutine you start ends the whole program unless that goroutine recovers. Wrap the work of long-lived workers so that one bad job does not take the process down.
Log the stack with runtime/debug.Stack() when you recover, because the original stack is gone once the panic is converted. Do not recover and continue after state corruption you cannot reason about.
Since Go 1.20 errors.Join combines multiple errors into one that still works with errors.Is and errors.As.
For concurrent work, golang.org/x/sync/errgroup runs goroutines and returns the first error while cancelling the shared context.
Retry only transient errors from idempotent operations, with a limit, backoff and context cancellation. Return permanent errors such as validation failures immediately.
No. recover only stops panics. Returned errors are handled with ordinary checks.
It must be called directly inside a deferred function, and in the same goroutine that panicked.
Wrap with fmt.Errorf("step name: %w", err) at each layer so the final message reads like a path through the call chain.
Recover from failures in Go by classifying them first. Fall back on expected errors, retry transient ones with backoff and context, release resources with defer, join batch errors with errors.Join, and use recover only at goroutine or request boundaries to turn bugs into logged errors.