Code with Go
About
  • Code With Go! 🚀
  • Hello World
  • Values
  • Variables
  • Constants
  • For Loop
  • If Else
  • Switch
  • Array
  • Slice
  • Map
  • Range
  • Functions
  • Multiple Return Values
  • Variadic Functions
  • Closure
  • Recursion
  • Pointers
  • Strings and Runes
  • Structs
  • Methods
  • Interfaces
  • Struct Embedding
  • Generics
  • Error
  • Goroutines
  • Channels
  • Channel Buffering
  • Channel Synchronization
  • Channel Directions
  • Select
  • Timeouts
  • Non-Blocking Channel Operations
  • Closing Channels
  • Range over Channels
  • Timers
  • Tickers
  • Worker Pools
  • Wait Groups
  • Rate Limiting
  • Atomic Counters
  • Mutexes
  • Stateful Goroutines
  • Sorting
  • Sorting by Functions
  • Panic
  • Defer
  • Recover
  • String Functions
  • String Formatting
  • Text Templates
  • Regular Expressions
  • JSON
  • XML
  • Time
  • Epoch
  • Time Formatting / Parsing
  • Random Numbers
  • Number Parsing
  • SHA256 Hashes
  • Base64 Encoding
  • Reading Files
  • Writing Files
  • Line Filters
  • File Paths
  • Directories
  • Temporary Files and Directories
  • Embed Directive
  • Testing and Benchmarking
  • Command-Line Arguments
  • Command-Line Flags
  • Command-Line Subcommands
  • Environment Variables
  • Logging
  • HTTP Client
  • HTTP Server
  • Context
  • Spawning Processes
  • Exec'ing Processes
  • Signals
  • Exit
  • Resize Image
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub

Recover

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.
    mayPanic()

    // This line will be reached only if there is no panic in the mayPanic function.
    fmt.Println("After mayPanic()")
}

output

Recovered. Error:
 a problem

Explanation:

  1. mayPanic():

    • This function deliberately panics with the custom error message "a problem."

  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. 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. mayPanic():

    • This line calls the mayPanic function, which intentionally panics.

  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.

PreviousDeferNextString Functions

Last updated 1 year ago

Was this helpful?