Documentation
¶
Overview ¶
Package ctxutil provides utilities for working with context.Context, including typed key-value storage and deadline helpers.
Index ¶
- func Deadline(ctx context.Context, maxDuration time.Duration) time.Duration
- func IsExpired(ctx context.Context) bool
- func Merge(ctx1, ctx2 context.Context) (context.Context, context.CancelFunc)
- func MustValue[T any](ctx context.Context, key string) T
- func Value[T any](ctx context.Context, key string) (T, bool)
- func ValueOr[T any](ctx context.Context, key string, defaultVal T) T
- func WithValue(ctx context.Context, key string, val any) context.Context
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Deadline ¶
Deadline returns the remaining time until the context deadline. If no deadline is set, returns maxDuration.
func IsExpired ¶
IsExpired returns true if the context has been cancelled or its deadline has passed.
func Merge ¶
Merge creates a context that is cancelled when either ctx1 or ctx2 is cancelled. Values are looked up from ctx1 first, then ctx2.
func MustValue ¶
MustValue retrieves a typed value from context. Panics if the key is absent or the type doesn't match.
func Value ¶
Value retrieves a typed value from context. Returns the zero value of T and false if the key is absent or the type doesn't match.
Types ¶
This section is empty.