You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
...packages should define keys as an unexported type to avoid
collisions.
// Package user defines a User type that's stored in Contexts.package user
import"context"// User is the type of value stored in the Contexts.typeUserstruct {...}
// key is an unexported type for keys defined in this package.// This prevents collisions with keys defined in other packages.typekeyint// userKey is the key for user.User values in Contexts. It is// unexported; clients use user.NewContext and user.FromContext// instead of using this key directly.varuserKeykey// NewContext returns a new Context that carries value u.funcNewContext(ctx context.Context, u*User) context.Context {
returncontext.WithValue(ctx, userKey, u)
}
// FromContext returns the User value stored in ctx, if any.funcFromContext(ctx context.Context) (*User, bool) {
u, ok:=ctx.Value(userKey).(*User)
returnu, ok
}
The text was updated successfully, but these errors were encountered:
See https://pkg.go.dev/context#Context:
...packages should define keys as an unexported type to avoid
collisions.
The text was updated successfully, but these errors were encountered: