---
source: https://iinventedlight.com/library/notes/go-defer-panic-recover.md
title: "Go defer, panic and recover"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "CC BY 4.0 (go.dev text); prose restates cited source lines"
attribution: "Summary by iinventedlight (AI-assisted). Sources: The Go Authors (go.dev), Defer, Panic, and Recover - The Go Programming Language, CC BY 4.0 (go.dev text); The Go Authors (go.dev), Effective Go - The Go Programming Language, CC BY 4.0 (go.dev text)."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-04
status: published
checked_on: 2026-11-04
as_of: 2026-11-04
confidence: medium
untested: "No code was run for this note; only the fetched documentation pages were read."
category: coding-references
topic_area: "code-ai-eng: Go"
tags: ['go', 'errors', 'defer', 'panic']
sources:
  - id: S1
    title: "Defer, Panic, and Recover - The Go Programming Language"
    publisher: The Go Authors (go.dev)
    url: https://go.dev/blog/defer-panic-and-recover
    accessed: 2026-11-04
    license: "CC BY 4.0 (go.dev text)"
  - id: S2
    title: "Effective Go - The Go Programming Language"
    publisher: The Go Authors (go.dev)
    url: https://go.dev/doc/effective_go
    accessed: 2026-11-04
    license: "CC BY 4.0 (go.dev text)"
---
# Go defer, panic and recover

**Answer:** In Go, `defer` schedules a function call to run when the surrounding function returns, which makes cleanup like closing files easy. `panic` stops normal execution and unwinds the stack, running deferred calls; `recover`, called inside a deferred function, stops a panic and lets the program continue.

## Three rules of defer

1. A deferred call's arguments are evaluated when the `defer` statement runs, not when the call happens.
2. Deferred calls run in last-in, first-out order after the function returns.
3. Deferred functions can read and set the function's named return values.

```go
f, err := os.Open(name)
if err != nil { return err }
defer f.Close()
```


## panic and recover

```go
defer func() {
    if r := recover(); r != nil {
        log.Println("recovered:", r)
    }
}()
```

`recover` only has an effect inside a deferred function during a panic; otherwise it returns nil.

## Claims
- id: C1
  claim: A deferred function's arguments are evaluated when defer is evaluated.
  source: S1
  evidence: "A deferred function’s arguments are evaluated when the defer statement is evaluated."
- id: C2
  claim: Deferred calls execute in Last In First Out order.
  source: S1
  evidence: "Deferred function calls are executed in Last In First Out order after the surrounding function"
- id: C3
  claim: Deferred functions may assign named return values.
  source: S1
  evidence: "Deferred functions may read and assign to the returning function’s named return values."
- id: C4
  claim: Defer is commonly used to simplify clean-up.
  source: S1
  evidence: "Defer is commonly used to simplify functions that perform various clean-up actions."
- id: C5
  claim: Effective Go says deferred functions execute in LIFO order.
  source: S2
  evidence: "Deferred functions are executed in LIFO order, so this code will cause"

## Sources
- [S1] Defer, Panic, and Recover - The Go Programming Language. The Go Authors (go.dev). https://go.dev/blog/defer-panic-and-recover . License: CC BY 4.0 (go.dev text). Accessed 2026-11-04.
- [S2] Effective Go - The Go Programming Language. The Go Authors (go.dev). https://go.dev/doc/effective_go . License: CC BY 4.0 (go.dev text). Accessed 2026-11-04.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted).
