Golang获取goroutine ID

版权声明:版权声明:本文为邵聪聪(聪少)原创文章,请随意转载并注明出处。 https://blog.csdn.net/cto_scc/article/details/53377733

Golang获取goroutine ID


package main

import (
    "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
}

猜你喜欢

转载自blog.csdn.net/cto_scc/article/details/53377733