package xuser import ( "context" "github.com/gin-gonic/gin" ) func GetUserFromContext(ctx context.Context) string { v := ctx.Value("username") if v == nil { return "" } return v.(string) } func SetContextUser(ctx *gin.Context, username string) { ctx.Set("username", username) //return context.WithValue(ctx, requestUserIdKey{}, username) } type userIdKey struct { } func WithContext(ctx context.Context, userId int64) context.Context { return context.WithValue(ctx, userIdKey{}, userId) } func FromContext(ctx context.Context) int64 { if v, ok := ctx.Value(userIdKey{}).(int64); ok { return v } return 0 }