goutil

package
v1.2.3 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2025 License: Apache-2.0 Imports: 5 Imported by: 0

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

View Source
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

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

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.

func GoFunc

func GoFunc(f func()) *Status

GoFunc runs a goroutine safely with panic recovery. It ensures the process does not crash due to an uncaught panic in the goroutine.

func (*Status) Wait

func (s *Status) Wait()

Wait waits for the goroutine to finish.

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

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

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.

Jump to

Keyboard shortcuts

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