Golang获取goroutine ID

来源:互联网 发布:上帝 知乎 编辑:程序博客网 时间:2024/05/30 23:24

Golang获取goroutine ID

package mainimport (    "bytes"    "fmt"    "runtime"    "strconv")func main() {    fmt.Println(getGID())}func getGID() uint64 {    b := make([]byte, 64)    b = b[:runtime.Stack(b, false)]    b = bytes.TrimPrefix(b, []byte("goroutine "))    b = b[:bytes.IndexByte(b, ' ')]    n, _ := strconv.ParseUint(string(b), 10, 64)    return n}
1 0
原创粉丝点击