Files
tamabox/internal/shared/goutil.go
T
2026-09-08 10:10:44 +08:00

21 lines
469 B
Go

package shared
import (
"github.com/sirupsen/logrus"
)
// SafeGo 在独立 goroutine 中执行 fn 并捕获 panic,防止任一 goroutine
// panic 未恢复导致整个进程崩溃(Go 默认行为:未恢复的 panic 终止进程)。
//
// 用法:shared.SafeGo(func() { ... })
func SafeGo(fn func()) {
go func() {
defer func() {
if r := recover(); r != nil {
logrus.WithField("panic", r).Error("goroutine panic recovered")
}
}()
fn()
}()
}