Skip to content

Panic and recover

  • panic stops normal execution, runs deferred functions, then the program terminates.
  • recover is a built‑in function that regains control after a panic. It must be called inside a deferred function.

Use only for unrecoverable errors (e.g., inconsistent state). For normal errors, prefer the error return pattern.

Catch a panic and continue.

package main
import "fmt"
func main() {
// Function that may panic
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered from:", r)
}
}()
fmt.Println("Before panic")
panic("a serious error")
fmt.Println("This will not be executed")
}
Terminal window
Before panic
Recovered from: a serious error

The program does not crash; it continues after recover. This can be useful for web servers (prevent a single request from crashing the whole server).