github.com/sitano/gsysint@v0.0.0-20190607084937-69a4f3233e4e/gid.go (about) 1 package gsysint 2 3 import ( 4 "errors" 5 "runtime" 6 "strconv" 7 "strings" 8 ) 9 10 var ErrWrongFormat = errors.New("wrong stack format") 11 12 // GIDFromStackTrace returns id of invoker goroutine. 13 func GIDFromStackTrace() (uint64, error) { 14 buf := make([]byte, 64) 15 sz := runtime.Stack(buf, false) 16 sp := strings.Split(string(buf[:sz]), " ") 17 if len(sp) < 2 { 18 return 0, ErrWrongFormat 19 } 20 id, err := strconv.ParseUint(sp[1], 10, 64) 21 if err != nil { 22 return 0, err 23 } 24 if id < 1 { 25 return 0, ErrWrongFormat 26 } 27 return id, nil 28 }