Built-in function

Go panic()

Interrupts normal control flow in the current goroutine and begins unwinding deferred calls. If no eligible deferred recovery occurs, the program terminates. Since Go 1.21, panic(nil) normally produces a non-nil recovery value; legacy compatibility settings can affect this behavior. Use returned errors for expected failures. Example recovers and prints invalid state.

Formal Syntax & Structure

go syntaxgo
panic(value)

Parameters & Attributes

NameTypeDescription
valueanyValue passed to panic.

Working Example

Example Snippetgo
package main
import "fmt"
func main() {  defer func() { fmt.Println(recover()) }()  panic("invalid state")}
Go Reference