elliotxx/safe
is a library for safely using go-routine, inspired by safe!
go get -u github.com/elliotxx/safe
package main
import "github.com/elliotxx/safe"
func doSomething() {
panic("BOOM!")
}
func customRecoverHandler(r interface{}) {
log.Printf("Recovered from %v", r)
}
func main() {
// Go starts a recoverable goroutine.
safe.Go(
doSomething,
)
// GoR starts a recoverable goroutine using given recover handler.
safe.GoR(
doSomething,
customRecoverHandler
)
// DefaultHandleCrash simply catches a crash with the default recover handler.
go func() {
defer safe.DefaultHandleCrash()
doSomething()
}()
// HandleCrash catches a crash with the custom recover handlers.
go func() {
defer safe.HandleCrash(customRecoverHandler)
doSomething()
}()
}