panic stops normal execution of the current goroutine and starts unwinding the stack, running deferred functions along the way. recover called inside a deferred function stops that unwinding and returns the value passed to panic. Use ordinary error values for expected failures, and reserve panic and recover for programmer errors and for protecting boundaries such as servers.
A panic starts when you call the built-in panic(v) or when the runtime hits an error such as a nil pointer dereference, an out-of-range index or integer division by zero. The goroutine then runs its deferred calls in last-in, first-out order. If nothing recovers, the program prints the panic value and a stack trace and exits with status 2.
A panic affects only its own goroutine for recovery purposes, but an unrecovered panic in any goroutine terminates the whole process.
recover only works when called directly by a deferred function. Called anywhere else, it returns nil.
The function uses a named err result so the deferred closure can turn the panic into a normal error. After a recovered panic the function returns to its caller as usual, and the program continues.
| Situation | Use |
|---|---|
| File missing, bad user input, network failure | Return an error |
| Impossible state, broken invariant, programmer bug | panic |
| Package initialisation cannot proceed (for example a bad compile-time regexp) | panic, as in regexp.MustCompile |
| Top of a goroutine, HTTP handler or worker loop | recover and log |
Go convention is that panics do not cross package boundaries. If your library uses panic internally, recover it in the exported function and return an error.
net/http already recovers panics in handlers and logs them, so one bad request does not kill the server. For your own goroutines, you must recover yourself, because a panic there crashes the entire program.
Log debug.Stack() when you recover, otherwise the origin of the bug is lost. errgroup and similar helpers do not recover panics for you either.
Although net/http recovers handler panics for you, many teams add their own middleware to return a clean 500 response and log with context. This is the standard place to use recover in a web service.
The check for http.ErrAbortHandler matters because that value is a deliberate panic used to abort a response, and it should not be logged as a bug.
If you recover something you cannot handle, panic again with the same value. Runtime panics have the type runtime.Error, so you can check for them:
Since Go 1.21, calling panic(nil) panics with a *runtime.PanicNilError, so recover() returning nil reliably means no panic occurred.
It looks similar, but Go style does not use it for control flow. Errors are returned as values, and panics signal bugs or unrecoverable states.
No. recover works only in the goroutine that is panicking, from a deferred function there.
Yes. All deferred calls in the panicking goroutine run in reverse order as the stack unwinds, which is how resources such as files and locks get released.
No. Some runtime failures, such as concurrent map writes or running out of memory, are fatal errors that cannot be recovered.
Use error returns for expected failures. Use panic for broken invariants, and recover inside a deferred function at boundaries such as goroutine entry points to log the stack and keep the process alive. Remember that recover works only in the same goroutine and only when called directly from a deferred function.