From 41feacfcd031586dd3cc483d61c1868e5acf038f 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:54:29 +0800 Subject: [PATCH] - update get --- const.go | 2 +- context.go | 7 ++++++- gin_use.go | 7 ++++++- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/const.go b/const.go index cab14fb..dcd4a7a 100644 --- a/const.go +++ b/const.go @@ -1,3 +1,3 @@ package gotrace_id -const Version = "1.0.1" +const Version = "1.0.2" diff --git a/context.go b/context.go index 79d65ba..b088204 100644 --- a/context.go +++ b/context.go @@ -2,6 +2,7 @@ package gotrace_id import ( "context" + "fmt" "github.com/gin-gonic/gin" "go.dtapp.net/gostring" ) @@ -18,5 +19,9 @@ func SetGinTraceIdContext(c *gin.Context) context.Context { // GetTraceIdContext 通过上下文获取跟踪编号 func GetTraceIdContext(ctx context.Context) string { - return ctx.Value("trace_id").(string) + traceId := fmt.Sprintf("%v", ctx.Value("trace_id")) + if len(traceId) <= 0 { + return "" + } + return traceId } diff --git a/gin_use.go b/gin_use.go index 61226fd..2482981 100644 --- a/gin_use.go +++ b/gin_use.go @@ -1,6 +1,7 @@ package gotrace_id import ( + "fmt" "github.com/gin-gonic/gin" "go.dtapp.net/gostring" ) @@ -20,5 +21,9 @@ func SetGinTraceId() gin.HandlerFunc { // GetGinTraceId 通过gin中间件获取跟踪编号 func GetGinTraceId(c *gin.Context) string { - return c.MustGet("trace_id").(string) + traceId := fmt.Sprintf("%v", c.MustGet("trace_id")) + if len(traceId) <= 0 { + return "" + } + return traceId }