feat: update template
This commit is contained in:
18
internal/pkg/safego/safego.go
Normal file
18
internal/pkg/safego/safego.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package safego
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"runtime/debug"
|
||||
)
|
||||
|
||||
// Go 以协程执行 fn,并捕获 panic 记录日志,防止协程崩溃杀死整个进程
|
||||
func Go(fn func()) {
|
||||
go func() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
slog.Error("goroutine panic recovered", "panic", r, "stack", string(debug.Stack()))
|
||||
}
|
||||
}()
|
||||
fn()
|
||||
}()
|
||||
}
|
||||
Reference in New Issue
Block a user