3 Star 0 Fork 0

neuro-netw0rk/server-golib

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
context.go 1.03 KB
一键复制 编辑 原始数据 按行查看 历史
LeoWang 提交于 7个月前 . 整合golib和serverlib
package utils
import (
"context"
"github.com/gin-gonic/gin"
"github.com/smallnest/rpcx/share"
"github.com/spf13/cast"
)
// SetContextValue 上下文设置值
// 注意!如果是go默认的上下文,则需要重建ctx,注意接收,否则不生效
func SetContextValue(ctx context.Context, key string, value any) context.Context {
if c, ok := ctx.(*gin.Context); ok {
c.Set(key, value)
return c
} else if c, ok := ctx.(*share.Context); ok {
c.SetValue(key, value)
return c
}
return context.WithValue(ctx, key, value)
}
func GetContextValue(ctx context.Context, key string) (value any) {
if c, ok := ctx.(*gin.Context); ok {
value, _ = c.Get(key)
return
} else if c, ok := ctx.(*share.Context); ok {
return c.Value(key)
}
return ctx.Value(key)
}
func GetContextValueInt64(ctx context.Context, key string) (value int64) {
val := GetContextValue(ctx, key)
return cast.ToInt64(val)
}
func GetContextValueString(ctx context.Context, key string) (value string) {
val := GetContextValue(ctx, key)
return cast.ToString(val)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/neuro-netw0rk/server-golib.git
git@gitee.com:neuro-netw0rk/server-golib.git
neuro-netw0rk
server-golib
server-golib
v0.0.5-beta2

搜索帮助