- update get

master v1.0.1
李光春 2 years ago
parent 28daa77389
commit 6b30aded32

@ -1,3 +1,3 @@
package gotrace_id package gotrace_id
const Version = "1.0.0" const Version = "1.0.1"

@ -2,7 +2,6 @@ package gotrace_id
import ( import (
"context" "context"
"fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"go.dtapp.net/gostring" "go.dtapp.net/gostring"
) )
@ -19,5 +18,5 @@ func SetGinTraceIdContext(c *gin.Context) context.Context {
// GetTraceIdContext 通过上下文获取跟踪编号 // GetTraceIdContext 通过上下文获取跟踪编号
func GetTraceIdContext(ctx context.Context) string { func GetTraceIdContext(ctx context.Context) string {
return fmt.Sprintf("%s", ctx.Value("trace_id")) return ctx.Value("trace_id").(string)
} }

@ -1,16 +1,11 @@
package gotrace_id package gotrace_id
import ( import (
"context"
"testing" "testing"
) )
func TestContext(t *testing.T) { func TestContext(t *testing.T) {
ctx := CustomTraceIdContext()
ctx := context.Background()
t.Log(ctx) t.Log(ctx)
t.Log(GetTraceIdContext(ctx))
ctx = context.WithValue(ctx, "k1", "测试")
t.Log()
} }

@ -1,7 +1,6 @@
package gotrace_id package gotrace_id
import ( import (
"fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"go.dtapp.net/gostring" "go.dtapp.net/gostring"
) )
@ -21,5 +20,5 @@ func SetGinTraceId() gin.HandlerFunc {
// GetGinTraceId 通过gin中间件获取跟踪编号 // GetGinTraceId 通过gin中间件获取跟踪编号
func GetGinTraceId(c *gin.Context) string { func GetGinTraceId(c *gin.Context) string {
return fmt.Sprintf("%s", c.MustGet("trace_id")) return c.MustGet("trace_id").(string)
} }

Loading…
Cancel
Save