From 9cb7c6b33ae9bcdc6ae562c57978c4349d396210 Mon Sep 17 00:00:00 2001 From: Yun Date: Wed, 27 Dec 2023 18:30:31 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- runtimex.go | 23 +++++++++++++++++++++++ runtimex_test.go | 13 +++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 runtimex.go create mode 100644 runtimex_test.go 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()) + +}