In Go, return an error for any failure that can happen during normal operation, and use panic only for situations the program cannot sensibly continue from: programming bugs, broken invariants or failed start-up requirements. Bad user input, missing files, network failures and timeouts are errors. A nil map write or an index out of range is a panic, raised by the runtime. This guide explains go when to panic with clear rules and examples.
Go has no exceptions. Errors are values returned to the caller, so the control flow is visible in the code. panic stops the current function, runs deferred calls, and unwinds the goroutine's stack; if nothing recovers it, the whole program crashes.
A panic runs the deferred functions of every frame as it unwinds, prints the message and stack trace, and exits with status 2. It cannot be caught by a caller that merely checks error values.
If the caller can reasonably do something about the problem, return an error. These are the everyday cases:
Library code in particular should almost never panic on bad input, because the caller decides what failure means for their program.
If a condition can only be false because of a bug, a panic surfaces it loudly instead of letting corrupted state spread:
The standard library does this too: regexp.MustCompile, template.Must and slice indexing all panic on conditions that signal incorrect use.
Initialising a package-level value from a constant that you control is a case where failure means the source code is wrong:
MustCompile panics if the pattern is invalid. Because the pattern is a literal, that failure shows up the first time you run the program. Do not use Must functions with user input; use regexp.Compile and handle the error.
If a service cannot work without a configuration value, crashing at start is better than running broken. Many programs call log.Fatal in main for this. Prefer returning errors up to main and exiting there, since log.Fatal skips deferred functions.
recover stops a panic when called directly inside a deferred function:
Use it at boundaries where one failing unit must not take down the whole process: an HTTP server's per-request goroutine (the net/http server already recovers handler panics and logs them) or a worker pool. Do not use it to emulate exceptions inside your own packages.
Two rules apply. A panic in one goroutine cannot be recovered by another goroutine, so each goroutine that may panic needs its own deferred recover. And recover only works inside the deferred function itself, not in a function it calls.
| Aspect | Return error | panic |
|---|---|---|
| Typical cause | Expected, environmental failure | Bug, impossible state |
| Who handles it | The caller, explicitly | Nobody, unless recovered |
| Visible in signature | Yes | No |
| Cost to callers | Must check err | Can crash the program |
| Testable | Compare values | Needs recover in tests |
A useful test: could a correct program, given valid code, ever see this condition? If yes, return an error. If no, a panic documents the broken assumption.
runtime/debug.Stack() when recovering.Only for programmer errors, such as passing a nil value the documentation forbids, or for Must helpers that are clearly named. For input-dependent failures, return an error.
Yes, unwinding and recovering is slower than an ordinary return, but performance is not the main reason to avoid it. The real reason is that it hides control flow from callers.
Both end the program. log.Fatal prints a message and exits without running deferred calls; panic prints a stack trace. Returning an error to main is usually the cleanest.
Most, such as nil dereference and out-of-range index. Fatal runtime errors like concurrent map writes or running out of memory cannot be recovered.
Return errors for anything that can fail while the program is running correctly, and reserve panic for bugs, impossible states and failed initialisation of values you control. Recover only at well-defined boundaries such as per-request handlers, always log the stack, and never use panic as a substitute for exceptions.