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

鏈接

results matching ""

    No results matching ""