26 lines
442 B
Go
26 lines
442 B
Go
package util
|
|
|
|
import (
|
|
"context"
|
|
)
|
|
|
|
type ContextKey string
|
|
|
|
const (
|
|
ContextKeyToken ContextKey = "token"
|
|
)
|
|
|
|
// token 写入上下文
|
|
func SetTokenToContext(ctx context.Context, token string) context.Context {
|
|
return context.WithValue(ctx, ContextKeyToken, token)
|
|
}
|
|
|
|
// 从上下文获取token
|
|
func GetTokenFromContext(ctx context.Context) string {
|
|
token, ok := ctx.Value(ContextKeyToken).(string)
|
|
if !ok {
|
|
return ""
|
|
}
|
|
return token
|
|
}
|