70 Star 505 Fork 184

Lyric / gin-admin

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
context.go 912 Bytes
一键复制 编辑 原始数据 按行查看 历史
Lyric 提交于 2018-10-30 20:58 . 更新上下文
package util
import (
"context"
)
type (
traceIDContextKey struct{}
userIDContextKey struct{}
)
// NewTraceIDContext 创建跟踪ID上下文
func NewTraceIDContext(ctx context.Context, traceID string) context.Context {
return context.WithValue(ctx, traceIDContextKey{}, traceID)
}
// FromTraceIDContext 从上下文中获取跟踪ID
func FromTraceIDContext(ctx context.Context) string {
v := ctx.Value(traceIDContextKey{})
if v != nil {
if s, ok := v.(string); ok {
return s
}
}
return ""
}
// NewUserIDContext 创建用户ID上下文
func NewUserIDContext(ctx context.Context, userID string) context.Context {
return context.WithValue(ctx, userIDContextKey{}, userID)
}
// FromUserIDContext 从上下文中获取用户ID
func FromUserIDContext(ctx context.Context) string {
v := ctx.Value(userIDContextKey{})
if v != nil {
if s, ok := v.(string); ok {
return s
}
}
return ""
}
Go
1
https://gitee.com/lyric/gin-admin.git
git@gitee.com:lyric/gin-admin.git
lyric
gin-admin
gin-admin
v1.1.0

搜索帮助