panic
A Go run-time failure that stops normal execution, runs deferred functions and, unless recovered, crashes the program with a stack trace.
In more detail
Panics happen on programming mistakes such as an index out of range, a nil pointer dereference or writing to a nil map, and you can trigger one with panic(). They signal a broken invariant, not an expected failure; return an error for things like a missing file. A panic unwinds only the goroutine it happened in, but an unrecovered panic ends the whole program.
Example
panic("bad invariant")Related terms
- recover — The built-in Go function that, called inside a deferred function, stops a panic and returns the value it was raised with.
- defer — A Go statement that schedules a function call to run when the surrounding function returns, used for cleanup such as closing a file.
- invariant — A rule that must always hold for an object or system, such as a count never being negative.
- error — In Go, an ordinary value of the built-in error type that a function returns to report that something went wrong.
- stack trace — The list of function calls that were running when an error happened, printed to help a developer find where it came from; Python calls it a traceback.
Taught in
- Variables, Typing, and Zero Values — Concurrent Systems Programming with Go
- Pointers and Memory — Concurrent Systems Programming with Go
- Arrays, Slices, and Maps — Concurrent Systems Programming with Go
- Type Assertions and the Empty Interface — Concurrent Systems Programming with Go
- Panics and Recover — Concurrent Systems Programming with Go
- Channels and Synchronization — Concurrent Systems Programming with Go
- Parsing Arguments with flag — Concurrent Systems Programming with Go