main
 1package context
 2
 3import (
 4	"context"
 5
 6	"github.com/xlgmokha/x/pkg/x"
 7)
 8
 9type Key[T any] string
10
11func (self Key[T]) With(ctx context.Context, value T) context.Context {
12	return context.WithValue(ctx, self, value)
13}
14
15func (self Key[T]) From(ctx context.Context) T {
16	if value := ctx.Value(self); value != nil {
17		return value.(T)
18	}
19	return x.Zero[T]()
20}