safe

package
v0.3.7 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package safe 提供 panic 恢复相关的小工具:把 panic 转成带堆栈的 error, 以及安全启动 goroutine(panic 不会崩进程)。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Go

func Go(fn func(), onPanic func(error))

Go 在新 goroutine 中执行 fn,recover panic 并交给 onPanic(可为 nil), 避免一个后台 goroutine 的 panic 拖垮整个进程。

func Run

func Run(fn func() error) (err error)

Run 执行 fn,并把其中的 panic 转换为 *PanicError 返回。

Types

type PanicError

type PanicError struct {
	Value any
	Stack []byte
}

PanicError 包装一个被 recover 的 panic 值及其堆栈。

func (*PanicError) Error

func (e *PanicError) Error() string

func (*PanicError) Unwrap

func (e *PanicError) Unwrap() error

Unwrap 在 panic 值本身是 error 时暴露它,便于 errors.Is/As。

Jump to

Keyboard shortcuts

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