Go Programming: Understanding and Utilizing Recover

Okan Özşahin
3 min readSep 23, 2023

Go, a modern and efficient programming language widely used for developing safe and fast applications. However, it’s a fact of software development that errors are inevitable. Therefore, Go provides the panic and recover mechanisms to prevent program crashes and handle errors. In this article, you will learn about recover in the Go programming language, what it is, and how to use it effectively.

What is a Panic?

In Go, a panic occurs when a program encounters an unexpected error. Critical errors like dividing by zero or invalid memory access can trigger panics. When a panic occurs, the normal program flow is interrupted, and the program stops running.

The recover Function

Go provides the recover function to regain control of a program in the event of a panic and prevent it from crashing. When recover is called within a defer function, it captures panic information, such as error messages or exceptions, and allows the program to continue.

func doSomething() {
defer func() {
if r := recover(); r != nil {
fmt.Println("Panic recovered:", r)
}
}()

// Code that may cause a panic goes here
// ...
}

In the above example, if the doSomething function triggers a panic, the recover function captures this panic information and prints "Panic recovered". This way, you can regain control of your program and prevent it from crashing.

How to Use recover

To handle panics using recover, follow these steps:

  1. Use defer to protect an operation that may cause a panic.
  2. Call the recover function within a defer function, placing it around the code that may panic.

When to Use recover: Use Cases and Examples

1.Panic Control: You can use recover to catch panics that occur within a function or goroutine and prevent the program from crashing. This allows the program to gracefully handle error situations.

defer func() {
if r := recover(); r != nil {
fmt.Println("Panic recovered:", r)
}
}()

// Code that may cause a panic goes here

2.Error Handling: recover can be used to handle specific errors within a function. For example, you can use it to catch errors that may occur during a database operation and handle them appropriately.

func fetchDataFromDatabase() error {
defer func() {
if r := recover(); r != nil {
fmt.Println("Database error recovered:", r)
}
}()

// Fetch data from the database
// If an error occurs, signal it with a panic
}

3.Inter-Goroutine Communication: recover can be useful for handling errors in inter-goroutine communication. Particularly, when the main goroutine spawns multiple child goroutines, it's important to handle any errors that may occur in any of the child goroutines.

func worker() {
defer func() {
if r := recover(); r != nil {
fmt.Println("Error in worker:", r)
}
}()

// Code running inside the goroutine
}

func main() {
go worker()
// Other operations
}

4.Shutdown Procedures: You can use recover to clean up or release specific resources when a program is terminated. This may involve tasks such as closing files or terminating database connections.

func cleanup() {
if r := recover(); r != nil {
fmt.Println("Error during shutdown:", r)
}
// Clean up resources
}

func main() {
defer cleanup()
// Program execution
}

recover simplifies the capturing and handling of panics but should be used with care. It should only be used to handle unexpected errors, and it should be placed around code that may panic using defer.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Okan Özşahin
Okan Özşahin

Written by Okan Özşahin

Backend Developer at hop | Civil Engineer | MS Computer Engineering

No responses yet

Write a response