go项目脚手架
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
sorbet/pkg/misc/key.go

26 lines
425 B

package misc
import (
"context"
)
type Key[T any] struct {
Name string
}
func (k *Key[T]) Wrap(ctx context.Context, value *T) context.Context {
return context.WithValue(ctx, k, value)
}
func (k *Key[T]) Lookup(ctx context.Context) (*T, bool) {
t, ok := ctx.Value(k).(*T)
return t, ok
}
func (k *Key[T]) Value(ctx context.Context) *T {
t, ok := ctx.Value(k).(*T)
if !ok {
panic("not value found")
}
return t
}