go语言获取协程id
温馨提示:
本文最后更新于 2022年03月27日,已超过 1,055 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我。
package main
import (
"bytes"
"fmt"
"runtime"
"strconv"
"time"
)
func main() {
go func() {
gid := GetGid()
fmt.Printf("child goruntine1 gid:%v \n",gid)
}()
go func() {
gid := GetGid()
fmt.Printf("child goruntine2 gid:%v \n",gid)
}()
go func() {
gid := GetGid()
fmt.Printf("child goruntine3 gid:%v \n",gid)
}()
go func() {
gid := GetGid()
fmt.Printf("child goruntine4 gid:%v \n",gid)
}()
go func() {
gid := GetGid()
fmt.Printf("child goruntine5 gid:%v \n",gid)
}()
gid := GetGid()
fmt.Printf("main goruntine gid:%v \n",gid)
time.Sleep(time.Second)
}
func GetGid() (gid uint64) {
b := make([]byte, 64)
b = b[:runtime.Stack(b, false)]
b = bytes.TrimPrefix(b, []byte("goroutine "))
b = b[:bytes.IndexByte(b, ' ')]
n, err := strconv.ParseUint(string(b), 10, 64)
if err != nil {
panic(err)
}
return n
}
正文到此结束
- 本文标签: 编程语言
- 本文链接: https://www.php20.cn/article/355
- 版权声明: 本文由仙士可原创发布,转载请遵循《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权