Go Reference ยท Built-in Functions
Go panic() function
Reference for Go panic(): stop normal execution, run deferred calls while unwinding, the runtime errors that panic by themselves, and when to use it.
panic() stops the normal flow of the current goroutine. Deferred functions run as the stack unwinds, and if nothing recovers the panic, the program prints the panic value with a stack trace and exits with status 2.
Go uses ordinary error values for expected failures. panic is for situations that should not happen: a broken invariant, an impossible state, or a programming mistake such as indexing past the end of a slice. The runtime itself panics in those cases.
func mustPositive(n int) int { if n <= 0 { panic("n must be positive") } return n }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Syntax
func panic(v any)
| Name | Description |
|---|---|
v |
Any value. It is passed on to recover() and printed if the panic is not recovered. Strings and error values are most common. |
Returns: nothing; the function never returns normally.
Edge cases:
- Since Go 1.21,
panic(nil)makesrecover()return a*runtime.PanicNilErrorinstead ofnil. The//go:debug panicnil=1directive restores the old behavior. - If a deferred function panics while another panic is running, both are printed in the crash report.
- A panic in one goroutine cannot be recovered by another; an unrecovered panic ends the whole program.
Usage notes
- Runtime panics include index out of range, nil map write, nil pointer dereference, integer division by zero, failed type assertion, closing a closed channel, and unlocking an unlocked mutex. They carry values that implement
runtime.Error. - Libraries should return errors, not panic, for bad input or I/O failures. A small convention exception is
MustXxxfunctions (such astemplate.Mustorregexp.MustCompile) that panic for invalid constant input at program start. - Cleanup still happens. Deferred calls run, so
defer mu.Unlock()anddefer file.Close()release resources even when a panic passes through. - Exit status: an unrecovered panic exits with status 2 and writes
panic: ...plusgoroutine 1 [running]:and the trace to standard error. os.Exitis different: it ends the program immediately and does not run deferred calls.log.Panic/log.Panicflog the message and then panic with it.- Servers:
net/httprecovers a panic in a handler, logs it and closes the connection, so one bad request does not kill the server.
Related
recover(),defer, Panic and recover rules,errors.New()- Lessons: Panic and recover, Defer