Go语言宕机恢复(recover)——防止程序崩溃

package main

import (
"fmt"
"time"
)
func main(){

Go(func(){
fmt.Println("hello")
panic("一路向北")
})

time.Sleep(5 * time.Second)
}

func Go(x func()){
go func(){
//defer 拦截panic
defer func(){
if err := recover(); err != nil {
fmt.Println(err)
}
}()
x()
}()
 
posted @ 2021-03-30 09:47  hubb  阅读(162)  评论(0编辑  收藏  举报