I have a program with lots of goroutines, and I want to handle the panic globally. It looks like I have to add a recovery in each of the goroutines, is there a way to do it globally?
I wrote a test program and I add a recover() in main, but it won't catch the crash in other goroutines:
package main
import "log"
func bug2() {
panic("bug2()")
}
func bug1() {
go bug2()
}
func main() {
defer func() {
if r := recover(); r != nil {
log.Println("catched")
}
}()
bug1()
select {}
}
What's the best practice here?