Please use https://github.com/mitchellh/panicwrap as it does not use some nasty memory patching.
Handle panics globaly.
Golang lets you not handle panics in subroutines in an easy way.
This package uses dirty memory patching techniques to supply this feature.
Use with care!
- Send a panic to an error tracking service
- Gracefully close the application
- ...
package main
import (
"os"
"encoding/json"
"github.com/talon-one/go-panichandler"
)
func main() {
panichandler.OnPanic(func(v interface{}) interface{} {
fmt.Printf("Catched a panic: %v\n", v)
return v
})
ch := make(chan struct{})
go func() {
panic("Hello World")
ch <- struct{}{}
}()
<-ch
}