gf/util/gutil/gutil_goroutine.go

30 lines
926 B
Go
Raw Normal View History

2023-09-11 10:18:44 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gutil
import (
"context"
)
// Go creates a new asynchronous goroutine function with specified recover function.
//
// The parameter `recoverFunc` is called when any panic during executing of `goroutineFunc`.
// If `recoverFunc` is given nil, it ignores the panic from `goroutineFunc` and no panic will
// throw to parent goroutine.
2023-09-12 20:00:01 +08:00
//
// But, note that, if `recoverFunc` also throws panic, such panic will be thrown to parent goroutine.
func Go(
ctx context.Context,
goroutineFunc func(ctx context.Context),
recoverFunc func(ctx context.Context, exception error),
) {
2023-09-11 10:18:44 +08:00
if goroutineFunc == nil {
return
}
2023-09-12 20:00:01 +08:00
go TryCatch(ctx, goroutineFunc, recoverFunc)
2023-09-11 10:18:44 +08:00
}