一、参考
golang 学习系列目录——更新 ing
goid
二、根本应用
获取以后运行 goroutine
的 id
2.1 装置
GO111MODULE=on go get -u github.com/petermattis/goid
2.2 示例
1 package sync1
2
3 import (
4 "fmt"
5 "runtime"
6 "strconv"
7 "strings"
8 "testing"
9
10 "github.com/petermattis/goid"
11 )
12
13 func TestGetGoID(t *testing.T) {
14
15 var buf [64]byte
16 n := runtime.Stack(buf[:], false)
17 idField := strings.Fields(strings.TrimPrefix(string(buf[:n]), "goroutine"))[0]
18 id, err := strconv.Atoi(idField)
19 if err != nil {20 panic(fmt.Sprintf("can not get goroutine id: %v", err))
21 }
22 fmt.Println("get goroutine id:", id)
23 }
24
25 func TestGetGoID2(t *testing.T) {26 gid := goid.Get()
27 fmt.Println("get goroutine id is", gid)
28 }