Documentation ¶
Overview ¶
Package Goroutine provides a small wrapper around go's goroutines, in order to easily create panic safe goroutines. Starting a new goroutine without taking care of recovering from a possible panic in that goroutine itself could crash the whole application. Therefore, in case of a panic, triggered by the goroutine which was created by the Go method, the panic will be automatically recovered and the error will be notified via the done channel.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrPanicRecovered is returned when a goroutine has panicked. ErrPanicRecovered = &panicError{message: "panic in goroutine recovered", value: nil} // ErrRecoverFuncPanicRecovered is returned when the recover function of a goroutine has panicked. ErrRecoverFuncPanicRecovered = &panicError{message: "panic in recover function of goroutine recovered", value: nil} )
Functions ¶
func Go ¶
func Go(f func()) <-chan error
Go runs a function f in a separate goroutine, which does automatically handle the recovering from a panic within that goroutine.
Example ¶
// Instead of go func() { values := [3]int{1, 2, 3} for i := 0; i < 4; i++ { fmt.Println(values[i]) } }() // simply call Go(func() { values := [3]int{1, 2, 3} for i := 0; i < 4; i++ { fmt.Println(values[i]) } })
Output:
Example (WithInputParam) ¶
// Functions with input params need to be wrapped by an anonymous function. // Instead of go func(s string) { panic(s) }("Hello World") // simply call Go(func() { func(s string) { panic(s) }("Hello World") })
Output:
func SetDefaultRecoverFunc ¶
func SetDefaultRecoverFunc(rf RecoverFunc)
SetDefaultRecoverFunc can be used to override the defaultRecoverFunc which is used by Go method.
Note: If you pass nil as a RecoverFunc, the panic will be silently recovered.
Types ¶
type Goroutine ¶
type Goroutine struct {
// contains filtered or unexported fields
}
Goroutine type contains the function f to run within that goroutine and the recover function rf. The recover function rf will be called in case of a panic in f within that goroutine.
func New ¶
func New(f func()) *Goroutine
New creates a new panic safe Goroutine, with the defaultRecoverFunc as recover function.
Example ¶
err := <-New(func() { values := [3]int{1, 2, 3} for i := 0; i < 4; i++ { fmt.Println(values[i]) } }).Go() fmt.Println(err)
Output: 1 2 3 panic in goroutine recovered: runtime error: index out of range [3] with length 3
func (*Goroutine) Go ¶
The Go method starts a new goroutine which is panic safe. A possible panic will be recovered by the recover function, either set by SetDefaultRecoverFunc or WithRecover.
func (*Goroutine) WithRecover ¶
func (g *Goroutine) WithRecover(rf RecoverFunc) *Goroutine
WithRecover overrides the default recover function with rf.
Note: If you pass nil as a RecoverFunc, the panic will be silently recovered.
Example ¶
err := <-New(func() { values := [3]int{1, 2, 3} for i := 0; i < 4; i++ { fmt.Println(values[i]) } }).WithRecover(func(v interface{}, done chan<- error) { if err, ok := v.(error); ok { done <- err return } done <- fmt.Errorf("recovered: %v", v) }).Go() fmt.Println(err)
Output: 1 2 3 runtime error: index out of range [3] with length 3
type RecoverFunc ¶
type RecoverFunc func(v interface{}, done chan<- error)
The RecoverFunc type defines the signature of a recover function within a Goroutine.
func GetDefaultRecoverFunc ¶
func GetDefaultRecoverFunc() RecoverFunc
GetDefaultRecoverFunc returns the current default recover function for goroutines used by the Go method.