Documentation
¶
Overview ¶
Package contextkey provides type-safe context key management using Go generics. It wraps the standard context.Context value storage with compile-time type safety, eliminating the need for type assertions and reducing runtime errors.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Key ¶
type Key[T any] interface { // Get retrieves the value associated with this key from the context. // Returns the value and true if found, or the zero value and false if not found. Get(context.Context) (T, bool) // Set stores a value in the context and returns a new context with the value set. Set(context.Context, T) context.Context // Delete removes the value associated with this key from the context. // Returns a new context with the value removed. Delete(context.Context) context.Context // Take retrieves and removes the value in a single operation. // Returns the new context, the value, and whether the value was found. // If the value doesn't exist, returns the original context and the zero value. Take(context.Context) (context.Context, T, bool) // Update modifies the existing value using the provided update function. // The update function receives the previous value and whether it existed. // Returns a new context with the updated value. Update(ctx context.Context, update func(prev T, hasPrev bool) T) context.Context }
Key is a type-safe interface for storing and retrieving values in a context. It provides compile-time type safety for context values through Go generics.
Click to show internal directories.
Click to hide internal directories.