Golang / Golang Internals and Memory Management Interview Questions
How do panic and recover work in Go and when should you use them?
panic stops the normal execution of the current goroutine, unwinds the stack calling all deferred functions, and propagates up until it reaches the top of the goroutine's stack — at which point the runtime prints a stack trace and terminates the program. recover can intercept a panic but only inside a deferred function.
// Basic panic — causes runtime abort with stack trace
func mustPositive(n int) int {
if n <= 0 {
panic(fmt.Sprintf("expected positive, got %d", n))
}
return n
}
// recover — MUST be called inside a deferred function
func safeDiv(a, b int) (result int, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("recovered from panic: %v", r)
}
}()
result = a / b // panics if b == 0
return
}
r, err := safeDiv(10, 0)
fmt.Println(r, err) // 0 recovered from panic: runtime error: integer divide by zero
// recover() returns nil if not panicking
// It cannot recover a panic from a DIFFERENT goroutine
go func() {
panic("goroutine panic") // crashes the whole program
}()
// When to use panic vs error:
// panic — unrecoverable programming errors (index out of bounds, nil deref)
// or internal invariant violations that should never happen
// error — expected failure conditions (file not found, network timeout, bad input)
// Libraries should NEVER let panics propagate to callers
// Use recover at the public API boundary to convert to errorsThe canonical use of panic/recover in Go is the library boundary pattern: a library may use panic internally for control flow (e.g., a parser that panics on syntax error deep in a call stack), but the exported function wraps the entire body in a deferred recover and converts the panic to an error value. This keeps the panic/recover internal and never surprises callers.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
