Documentation ¶
Overview ¶
Panic describes and shows how panic and defer work.
Panics come from:
- runtime (e.g. out-of-bounds array access or nil pointer dereference)
- built-in panic function (for grave errors or impossible situations)
"Expected" errors (from incorrect input, misconfiguration, or failing I/O) should be handled using error values.
During panic
- normal execution stops
- all deferred function calls in that goroutine are executed in LIFO order
- the program crashes with panic value (usually an error message) + stack trace for each goroutine
Taken from: https://github.com/adonovan/gopl.io/blob/master/ch5/defer1/defer.go
Level: beginner Topics: panic, defer
Click to show internal directories.
Click to hide internal directories.