美文网首页
golang获取goroutine ID

golang获取goroutine ID

作者: 蔡欣圻 | 来源:发表于2019-08-29 13:58 被阅读0次

golang本身不提供获取goroutineID的接口,如果要获取goroutineID可以使用下面的方法

    package main

import (
    "bytes"
    "fmt"
    "runtime"
    "strconv"
)

func main() {
    fmt.Printf("goroutine ID is:%d\n", 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
}

相关文章

网友评论

      本文标题:golang获取goroutine ID

      本文链接:https://www.haomeiwen.com/subject/khdiectx.html