Skip to content

Latest commit

 

History

History
23 lines (19 loc) · 521 Bytes

18.6.md

File metadata and controls

23 lines (19 loc) · 521 Bytes

18.6 函数

如何使用内建函数 recover() 终止 panic() 过程(参考章节 13.3):

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()
}

链接