github.com/v2pro/plz@v0.0.0-20221028024117-e5f9aec5b631/gls/goid.go (about) 1 package gls 2 3 import ( 4 "unsafe" 5 "github.com/v2pro/plz/reflect2" 6 ) 7 8 // offset for go1.4 9 var goidOffset uintptr = 128 10 11 func init() { 12 gType := reflect2.TypeByName("runtime.g").(reflect2.StructType) 13 if gType == nil { 14 panic("failed to get runtime.g type") 15 } 16 goidField := gType.FieldByName("goid") 17 goidOffset = goidField.Offset() 18 } 19 20 // GoID returns the goroutine id of current goroutine 21 func GoID() int64 { 22 g := getg() 23 p_goid := (*int64)(unsafe.Pointer(g + goidOffset)) 24 return *p_goid 25 } 26 27 func getg() uintptr