Files
blog-server/internal/pkg/safego/safego.go
2026-08-19 22:05:49 +08:00

19 lines
349 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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()
}()
}