github.com/songzhibin97/gkit@v1.2.13/internal/sys/goid/gid.go (about) 1 package goid 2 3 import ( 4 "fmt" 5 "runtime" 6 "strconv" 7 "strings" 8 ) 9 10 // GetGID 获取goroutine唯一ID 11 func GetGID() int64 { 12 var buf [64]byte 13 n := runtime.Stack(buf[:], false) 14 // 得到id字符串 15 idField := strings.Fields(strings.TrimPrefix(string(buf[:n]), "goroutine "))[0] 16 id, err := strconv.Atoi(idField) 17 if err != nil { 18 panic(fmt.Sprintf("cannot get goroutine id: %v", err)) 19 } 20 return int64(id) 21 }