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