Skip to content
Tutorials Center
My learning

Topics

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.

Go
func mustPositive(n int) int {
	if n <= 0 {
		panic("n must be positive")
	}
	return n
}

Try it

Go Deferred calls run while unwinding

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.

Go Panics raised by the runtime

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

Go
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) makes recover() return a *runtime.PanicNilError instead of nil. The //go:debug panicnil=1 directive 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 MustXxx functions (such as template.Must or regexp.MustCompile) that panic for invalid constant input at program start.
  • Cleanup still happens. Deferred calls run, so defer mu.Unlock() and defer file.Close() release resources even when a panic passes through.
  • Exit status: an unrecovered panic exits with status 2 and writes panic: ... plus goroutine 1 [running]: and the trace to standard error.
  • os.Exit is different: it ends the program immediately and does not run deferred calls.
  • log.Panic/log.Panicf log the message and then panic with it.
  • Servers: net/http recovers a panic in a handler, logs it and closes the connection, so one bad request does not kill the server.