Documentation
¶
Overview ¶
Package goutil provides a safe way to execute goroutines with built-in panic recovery.
In practice, goroutines may panic due to issues like nil pointer dereference or out-of-bounds access. However, these panics can be recovered. This package offers a wrapper to safely run goroutines, ensuring that any panic is caught and passed to a user-defined `OnPanic` handler.
The `OnPanic` function allows developers to log the panic, report metrics, or perform other custom recovery logic, making it easier to manage and observe unexpected failures in concurrent code.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var OnPanic = func(ctx context.Context, r any) { fmt.Printf("panic: %v\n%s\n", r, debug.Stack()) }
OnPanic is a global callback function triggered when a panic occurs.
Functions ¶
This section is empty.
Types ¶
type Status ¶
type Status struct {
// contains filtered or unexported fields
}
Status provides a mechanism to wait for a goroutine to finish.
func Go ¶
Go runs a goroutine safely with context support and panic recovery. It ensures the process does not crash due to an uncaught panic in the goroutine.
type ValueStatus ¶
type ValueStatus[T any] struct { // contains filtered or unexported fields }
ValueStatus provides a mechanism to wait for a goroutine that returns a value and an error.
func GoValue ¶
GoValue runs a goroutine safely with context support and panic recovery and returns its result and error. It ensures the process does not crash due to an uncaught panic in the goroutine.
func (*ValueStatus[T]) Wait ¶
func (s *ValueStatus[T]) Wait() (T, error)
Wait blocks until the goroutine finishes and returns its result and error.