一、参考

二、基本使用

goroutineid

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 }