github.com/lingyao2333/mo-zero@v1.4.1/core/threading/routines.go (about) 1 package threading 2 3 import ( 4 "bytes" 5 "runtime" 6 "strconv" 7 8 "github.com/lingyao2333/mo-zero/core/rescue" 9 ) 10 11 // GoSafe runs the given fn using another goroutine, recovers if fn panics. 12 func GoSafe(fn func()) { 13 go RunSafe(fn) 14 } 15 16 // RoutineId is only for debug, never use it in production. 17 func RoutineId() uint64 { 18 b := make([]byte, 64) 19 b = b[:runtime.Stack(b, false)] 20 b = bytes.TrimPrefix(b, []byte("goroutine ")) 21 b = b[:bytes.IndexByte(b, ' ')] 22 // if error, just return 0 23 n, _ := strconv.ParseUint(string(b), 10, 64) 24 25 return n 26 } 27 28 // RunSafe runs the given fn, recovers if fn panics. 29 func RunSafe(fn func()) { 30 defer rescue.Recover() 31 32 fn() 33 }