Errors signal expected failures, conditions the caller should anticipate and handle. A panic signals that the program has entered a state it cannot recover from through normal means: an invariant has been violated, an impossible condition has been reached, or the runtime has detected something fundamentally wrong. This article covers when Go raises panics, how they propagate through the call stack, and what recover can and cannot do, which you need to know to write reliable programs and draw safe boundaries around a package.

What is a panic

A panic is an abrupt halt in a goroutine's normal execution. The Go runtime raises panics automatically for a narrow set of violations: accessing a slice or array index out of bounds, dereferencing a nil pointer, dividing an integer by zero, or writing to a closed channel.

Go
s := []int{1, 2, 3}_ = s[5]

Running this produces a runtime panic:

Terminal
panic: runtime error: index out of range [5] with length 3goroutine 1 [running]:main.main()        /tmp/main.go:6 +0x18exit status 2

The runtime prints the panic message, the goroutine that panicked, the exact file and line number, and exits with a non-zero status code.

You can also raise a panic explicitly using the built-in panic function, which accepts a value of type any:

Go
panic("critical: configuration not found")panic(fmt.Errorf("unexpected state: %v", state))panic(42)

The argument can be anything: a string, an error, a struct, a number. When panic is called, the current function stops immediately. No code after the call to panic runs. The runtime begins unwinding the stack.

Panic and defer

To understand panic, you need to know how it interacts with defer. As a panic unwinds the stack, it runs every deferred function in each stack frame before moving on to the next. Deferred functions execute in LIFO order within each frame, the same order they would run in on a normal return.

Go
func a() {    defer fmt.Println("defer in a")    b()}func b() {    defer fmt.Println("defer in b")    panic("something went wrong")}func main() {    defer fmt.Println("defer in main")    a()}
Terminal
defer in bdefer in adefer in mainpanic: something went wronggoroutine 1 [running]:main.b(...)        /tmp/main.go:10 +0x58main.a(...)        /tmp/main.go:5 +0x30main.main()        /tmp/main.go:15 +0x40exit status 2

After b panics, its deferred function runs first. The panic then moves up to a, which runs its deferred function. Finally it reaches main, runs its deferred function, and then prints the panic message and terminates.

Because deferred functions run even during a panic, defer is the right tool for resource cleanup. File handles, database connections, mutexes, and any other resources guarded with defer are released even if the program panics.

The recover function

recover is a built-in function that stops a panic from propagating and returns the value that was passed to panic. Its signature is:

Go
func recover() any

If called when no panic is occurring, recover returns nil. There is one strict requirement: recover must be called directly inside a deferred function. Calling it in a helper that is itself called from a deferred function, or anywhere outside of defer, has no effect.

The canonical pattern wraps recover in an anonymous deferred function:

Go
func safeRun(fn func()) (err error) {    defer func() {        if r := recover(); r != nil {            err = fmt.Errorf("panic: %v", r)        }    }()    fn()    return nil}

When fn panics, the deferred function runs, recover() captures the panic value, and safeRun returns normally with an error set. When fn does not panic, recover() returns nil and the deferred function does nothing.

What happens after recover

After recover catches a panic, the panicking goroutine does not resume the function that panicked. The deferred function containing recover completes normally, and control returns to the caller of the panicking function. The code after the panic in the original function never runs.

Go
func riskyOp() {    fmt.Println("before panic")    panic("something went wrong")    fmt.Println("this never runs")}func main() {    defer func() {        if r := recover(); r != nil {            fmt.Println("recovered:", r)        }    }()    riskyOp()    fmt.Println("this also never runs")}
Terminal
before panicrecovered: something went wrong

riskyOp stops at the panic. main never reaches its final Println. After the recovery, execution continues in the caller and never goes back to where the panic occurred.

When to use recover

The intended purpose of recover is narrow: preventing a panic inside one subsystem from crashing the entire process. The clearest use case is a package that executes code it does not fully control (a plugin system, a template engine, a request handler), where a panic in that code should become a returned error instead of bringing down the server.

Go
func Execute(fn func()) (err error) {    defer func() {        if r := recover(); r != nil {            err = fmt.Errorf("execution failed: %v", r)        }    }()    fn()    return nil}

Callers of Execute receive an error instead of a crash. The package acts as a safety boundary between untrusted or unpredictable code and the rest of the program.

This approach has two limits.

Recovery does not fix the underlying problem. If the condition that caused the panic still exists (the same nil pointer, the same out-of-bounds index) and the same code runs again, it will panic again. recover gives you a graceful exit, and it does not retry the failed operation.

Panic and recover are not exception handling. If you use panic to signal expected failures and recover to handle them, the way try-catch works in other languages, the code gets harder to read and to compose, and it goes against what anyone reading idiomatic Go expects. Return error for expected failures, let panics propagate for truly unrecoverable states, and use recover only at package boundaries, to contain the damage and turn it into an error the caller can act on.