go程Id retrieve the current goroutine's ID

https://github.com/petermattis/goid


func GoID() int {
var buf [64]byte
n := runtime.Stack(buf[:], false)
// 得到id字符串
idField := strings.Fields(strings.TrimPrefix(string(buf[:n]), "goroutine "))[0]
id, err := strconv.Atoi(idField)
if err != nil {
panic(fmt.Sprintf("cannot get goroutine id: %v", err))
}
return id
}

 

posted @ 2023-03-15 13:24  papering  阅读(21)  评论(0编辑  收藏  举报