---
title: "Defer, panic, and recover in Go."  
description: "Defer, panic, and recover in Go."  
author: "Sandra Emily"  
published: 2023-10-17  
updated: 2023-10-17  
canonical: https://www.mindstick.com/forum/160185/defer-panic-and-recover-in-go  
category: "go"  
tags: ["go", "golang"]  
reading_time: 2 minutes  

---

# Defer, panic, and recover in Go.

Defer, [panic](https://answers.mindstick.com/qa/40727/how-did-andrew-jackson-s-pet-banks-contributed-to-the-panic-of-1837), and [recover](https://www.mindstick.com/forum/159255/how-to-recover-a-file-deleted-with-python) in Go.

## Replies

### Reply by Aryan Kumar

In Go, **defer**, **panic**, and **recover** are mechanisms used for handling errors and managing the flow of control in exceptional situations. Let's discuss each of them in a humanized way:

**1. defer**:

- The **defer** keyword is used to schedule a function call to be executed just before the surrounding function returns.
- It's often used for cleanup or resource release tasks.
- Multiple **defer** statements are executed in a last-in, first-out (LIFO) order.
- **defer** is helpful for making sure resources like files or connections are properly closed, even if an error occurs.

Example:

```plaintext
func someFunction() {
    // This will be executed when someFunction returns
    defer fmt.Println("Cleanup task")

    // Some code that might cause an error
    // ...
}
```

**2. panic**:

- **panic** is a built-in function that is used to terminate normal execution of a Go program and enter a panic state.
- It's typically used for unrecoverable errors, like out-of-bounds array access or nil pointer dereference.
- When **panic** is called, it triggers the execution of any deferred functions (via **defer**) and then the program terminates.

Example:

```plaintext
func someFunction() {
    // Simulating a panic condition
    if somethingIsWrong {
        panic("Something went terribly wrong!")
    }
    // ...
}
```

**3. recover**:

- **recover** is a built-in function used to regain control after a **panic**.
- It's only meaningful to call **recover** in a deferred function.
- When **recover** is called within a deferred function, it stops the panic and returns the value that was passed to the **panic**.

Example:

```plaintext
func recoverFromPanic() {
    if r := recover(); r != nil {
        fmt.Println("Recovered from panic:", r)
    }
}

func main() {
    defer recoverFromPanic()

    // Simulate a panic
    panic("Oh no, panic occurred!")
}
```

In this example, when a panic occurs in the **main** function, it triggers the **recoverFromPanic** function through the deferred call. The **recoverFromPanic** function then uses **recover** to handle the panic and continue the program's execution.

These mechanisms are useful for gracefully handling errors and exceptional situations in Go, allowing you to maintain the integrity of your programs even in the face of unexpected issues.


---

Original Source: https://www.mindstick.com/forum/160185/defer-panic-and-recover-in-go

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
