safego

package
v0.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 14, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package safego provides utilities for running goroutines safely with built-in panic recovery.

Goroutines may panic due to programming errors such as nil pointer dereferences or out-of-bounds access. While such panics would normally crash the entire process, they can be recovered inside the goroutine.

This package offers wrappers that launch goroutines with automatic panic recovery. When a panic is recovered, a global OnPanic handler is invoked. This allows applications to log panics, emit metrics, or trigger alerts, making failures in concurrent code easier to observe and debug.

Index

Constants

This section is empty.

Variables

View Source
var OnPanic = func(ctx context.Context, info PanicInfo) {
	fmt.Printf("[PANIC] %v\n%s\n", info.Panic, info.Stack)
}

OnPanic is a global callback invoked whenever a panic is recovered inside a goroutine launched by this package.

By default, it prints the panic value and stack trace to stdout. Applications may override this function during initialization to provide custom logging, metrics, or alerting behavior.

Functions

This section is empty.

Types

type GoValueFunc

type GoValueFunc[T any] func(ctx context.Context) (T, error)

type PanicInfo

type PanicInfo struct {
	Panic any
	Stack []byte
}

PanicInfo contains information about a recovered panic.

type Status

type Status struct {
	// contains filtered or unexported fields
}

Status represents the execution status of a goroutine and provides a mechanism to wait for its completion.

func Go

func Go(ctx context.Context, f func(ctx context.Context), withoutCancel bool) *Status

Go launches a goroutine that executes the provided function f with panic recovery enabled. If the goroutine panics, the panic is recovered and the global OnPanic handler is invoked.

The provided context is passed to both f and OnPanic. The goroutine does not automatically stop when the context is canceled; the function f is responsible for observing ctx.Done() and returning when appropriate.

If withoutCancel is true, the goroutine receives a context that is not canceled when the parent context is canceled.

func (*Status) Wait

func (s *Status) Wait()

Wait blocks until the associated goroutine completes.

type ValueStatus

type ValueStatus[T any] struct {
	// contains filtered or unexported fields
}

ValueStatus represents the execution status of a goroutine that returns a value and an error.

func GoValue

func GoValue[T any](ctx context.Context, f GoValueFunc[T], withoutCancel bool) *ValueStatus[T]

GoValue launches a goroutine that executes the provided function f, captures its returned value and error, and recovers from panics. If a panic occurs, it is recovered, reported via the global OnPanic handler, and converted into an error returned by Wait.

The provided context is passed to both f and OnPanic. As with Go, cancellation is cooperative: f must observe ctx.Done() if early termination is desired.

If withoutCancel is true, the goroutine receives a context that is not canceled when the parent context is canceled.

func (*ValueStatus[T]) Wait

func (s *ValueStatus[T]) Wait() (T, error)

Wait blocks until the goroutine completes and returns its value and error.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL