From 6b30aded32a88f706eb470706027c49c41d30ab4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=85=89=E6=98=A5?= Date: Sat, 13 Aug 2022 13:44:37 +0800 Subject: [PATCH] - update get --- const.go | 2 +- context.go | 3 +-- context_test.go | 9 ++------- gin_use.go | 3 +-- 4 files changed, 5 insertions(+), 12 deletions(-) diff --git a/const.go b/const.go index 8ee3b7d..cab14fb 100644 --- a/const.go +++ b/const.go @@ -1,3 +1,3 @@ package gotrace_id -const Version = "1.0.0" +const Version = "1.0.1" diff --git a/context.go b/context.go index e75ef0d..79d65ba 100644 --- a/context.go +++ b/context.go @@ -2,7 +2,6 @@ package gotrace_id import ( "context" - "fmt" "github.com/gin-gonic/gin" "go.dtapp.net/gostring" ) @@ -19,5 +18,5 @@ func SetGinTraceIdContext(c *gin.Context) context.Context { // GetTraceIdContext 通过上下文获取跟踪编号 func GetTraceIdContext(ctx context.Context) string { - return fmt.Sprintf("%s", ctx.Value("trace_id")) + return ctx.Value("trace_id").(string) } diff --git a/context_test.go b/context_test.go index c38c1da..5e42468 100644 --- a/context_test.go +++ b/context_test.go @@ -1,16 +1,11 @@ package gotrace_id import ( - "context" "testing" ) func TestContext(t *testing.T) { - - ctx := context.Background() + ctx := CustomTraceIdContext() t.Log(ctx) - - ctx = context.WithValue(ctx, "k1", "测试") - - t.Log() + t.Log(GetTraceIdContext(ctx)) } diff --git a/gin_use.go b/gin_use.go index 95d222d..61226fd 100644 --- a/gin_use.go +++ b/gin_use.go @@ -1,7 +1,6 @@ package gotrace_id import ( - "fmt" "github.com/gin-gonic/gin" "go.dtapp.net/gostring" ) @@ -21,5 +20,5 @@ func SetGinTraceId() gin.HandlerFunc { // GetGinTraceId 通过gin中间件获取跟踪编号 func GetGinTraceId(c *gin.Context) string { - return fmt.Sprintf("%s", c.MustGet("trace_id")) + return c.MustGet("trace_id").(string) }