0%

2023-04-18-14-43-49

获取gorountine id

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
func GetGoid() int64 {
var (
buf [64]byte
n = runtime.Stack(buf[:], false)
stk = strings.TrimPrefix(string(buf[:n]), "goroutine")
)

idField := strings.Fields(stk)[0]
id, err := strconv.Atoi(idField)
if err != nil {
panic(fmt.Errorf("can not get goroutine id: %v", err))
}

return int64(id)
}