> For the complete documentation index, see [llms.txt](https://go.xiao5.info/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://go.xiao5.info/di-si-bu-fen-shi-ji-ying-yong/18.0/18.6.md).

# 函数

如何使用内建函数 `recover()` 终止 `panic()` 过程（参考[章节 13.3](/di-san-bu-fen-go-gao-ji-bian-cheng/13.0/13.3.md)）：

```go
func protect(g func()) {
    defer func() {
        log.Println("done")
        // Println executes normally even if there is a panic
        if x := recover(); x != nil {
            log.Printf("run time panic: %v", x)
        }
    }()
    log.Println("start")
    g()
}
```

## 链接

* [目录](https://github.com/MartialBE/the-way-to-go_ZH_CN/blob/master/eBook/directory.md)
* 上一节：[接口](/di-si-bu-fen-shi-ji-ying-yong/18.0/18.5.md)
* 下一节：[文件](/di-si-bu-fen-shi-ji-ying-yong/18.0/18.7.md)
