14.6 协程和恢复(recover)
...小于 1 分钟
14.6 协程和恢复(recover)
恢复是在 panicking 的协程内部的:不能被另外一个协程恢复
func server(workChan <-chan *Work) {
for work := range workChan {
go safelyDo(work) // start the goroutine for that work
}
}
func safelyDo(work *Work) {
defer func() {
if err := recover(); err != nil {
log.Printf("Work failed with %s in %v", err, work)
}
}()
do(work)
}
如果 do(work)
发生 panic()
,错误会被记录且协程会退出并释放,而其他协程不受影响。
Powered by Waline v2.15.2