mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 01:55:35 +08:00
24 lines
521 B
Markdown
24 lines
521 B
Markdown
# 18.6 函数
|
||
|
||
如何使用内建函数 `recover()` 终止 `panic()` 过程(参考[章节 13.3](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()
|
||
}
|
||
```
|
||
|
||
## 链接
|
||
|
||
- [目录](directory.md)
|
||
- 上一节:[接口](18.5.md)
|
||
- 下一节:[文件](18.7.md)
|