Code with Go
Ask or search…


This Go code demonstrates the use of the recover function to catch and handle panics within a deferred function. Let's go through the code with inline comments and explanations:
// Importing necessary package.
import "fmt"
// mayPanic function deliberately panics with a custom error message.
func mayPanic() {
panic("a problem")
// The main function, where the execution of the program begins.
func main() {
// Using a deferred anonymous function to recover from panics.
defer func() {
// The recover function is used to catch a panic.
if r := recover(); r != nil {
// Handling the panic by printing the recovered error message.
fmt.Println("Recovered. Error:\n", r)
// Calling the mayPanic function, which panics intentionally.
// This line will be reached only if there is no panic in the mayPanic function.
fmt.Println("After mayPanic()")


Recovered. Error:
a problem
  1. 1.
    • This function deliberately panics with the custom error message "a problem."
  2. 2.
    defer func() { ... }():
    • The defer statement is used to schedule the execution of an anonymous function to be performed when the surrounding function (main in this case) exits.
    • The anonymous function includes a recover() call, which returns nil if there is no panic or the value passed to panic if a panic occurred.
  3. 3.
    if r := recover(); r != nil { ... }:
    • Inside the deferred function, the recover function is used to catch a panic.
    • If a panic occurred, the recovered value (error message in this case) is printed.
  4. 4.
    • This line calls the mayPanic function, which intentionally panics.
  5. 5.
    fmt.Println("After mayPanic()"):
    • If there was no panic or if the panic was recovered successfully, this line will be executed.
    • If a panic occurred and was caught by the deferred function, the program will continue executing from this point.
Using recover in a deferred function is a common pattern for handling panics and performing cleanup or logging operations before the program exits. It allows you to gracefully recover from unexpected errors and continue with the execution of the program.