commit 9cb7c6b33ae9bcdc6ae562c57978c4349d396210 Author: Yun Date: Wed Dec 27 18:30:31 2023 +0800 提交 diff --git a/runtimex.go b/runtimex.go new file mode 100644 index 0000000..75edfe9 --- /dev/null +++ b/runtimex.go @@ -0,0 +1,23 @@ +package runtimex + +import ( + "bytes" + "runtime" + "strconv" +) + +// 2023年6月30日19:26:09 + +// 获取goroutine_id +func GetGid() (gid uint64) { + b := make([]byte, 64) + b = b[:runtime.Stack(b, false)] + b = bytes.TrimPrefix(b, []byte("goroutine ")) + b = b[:bytes.IndexByte(b, ' ')] + n, err := strconv.ParseUint(string(b), 10, 64) + if err != nil { + panic(err) + } + return n +} + diff --git a/runtimex_test.go b/runtimex_test.go new file mode 100644 index 0000000..c91a513 --- /dev/null +++ b/runtimex_test.go @@ -0,0 +1,13 @@ +package runtimex_test + +import ( + "testing" + + "code.yun.ink/pkg/runtimex" +) + +func TestGetgId(t *testing.T) { + t.Log("测试获取goroutine_id") + t.Log("goroutine_id:", runtimex.GetGid()) + +}