> For the complete documentation index, see [llms.txt](https://go.netdpi.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://go.netdpi.net/functions/defer.md).

# 07.06-Defer, Panic & Recover

Go 有一個特殊的陳述句，稱為 `defer` 會在一個函式執行完畢之後，將該函式呼叫加入排班，請見下列的範例：

```go
package main

import "fmt"

func first() {
    fmt.Println("1st")
}
func second() {
    fmt.Println("2nd")
}
func main() {
    defer second()
    first()
}
```

此程式會先印出 `1st`，並接著印出 `2nd`，基本上 defer 會將呼叫移到函式尾端的 `second`。

```go
func main() {
    first()
    second()
}
```

`defer` 通常用於需要以某種方式釋放資源時，例如：當我們開啟一個檔案時，我們需要能夠確定之後會關閉檔案。使用 `defer`：

```go
f, _ := os.Open(filename)
defer f.Close()
```

這樣有三項優點：(1) 讓我們的 `Close` 呼叫可以靠近我們的 `Open` 呼叫，使得易於了解呼叫的用處 (2) 若我們的函式有多個 return 陳述句時（或許 `if` 裡面有一個，而 `else` 裡面也有一個） `Close` 會在這兩個 return 之前執行 (3) 受到 defer 的函式即使在執行期當掉了，也能運作。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://go.netdpi.net/functions/defer.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
