代码拉取完成,页面将自动刷新
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)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。