threadpkg

package
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: May 15, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

README

thread

goroutine 安全执行和 recover 包装。

基础用法

threadpkg.GoSafe(func() {
	// async work
})
threadpkg.GoSafeWithContext(ctx, func(ctx context.Context) {
	// async work with context
})
threadpkg.GoWithContext(ctx, func(ctx context.Context) {
	// alias of GoSafeWithContext
})

注意事项

异步任务仍需处理 context、超时、日志和资源释放。

验证

go test ./thread

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Go

func Go(fn func())

Go runs the given fn using another goroutine, recovers if fn panics.

func GoSafe

func GoSafe(fn func())

GoSafe runs the given fn using another goroutine, recovers if fn panics.

func GoSafeWithContext

func GoSafeWithContext(ctx context.Context, fn func(ctx context.Context))

GoSafeWithContext ...

func GoWithContext

func GoWithContext(ctx context.Context, fn func(ctx context.Context))

GoWithContext runs the given fn using another goroutine with context, recovers if fn panics.

func Recover

func Recover(ctx context.Context)

Recover is used with defer to do cleanup on panics. Use it like: defer Recover(func() {})

func RecoverNotContext

func RecoverNotContext()

RecoverNotContext is used with defer to do cleanup on panics. Use it like: defer Recover(func() {})

Types

This section is empty.

Jump to

Keyboard shortcuts

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