contextext

package
v1.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 28, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidContext   = errors.Base("invalid context")
	ErrInvalidValueMap  = errors.Base("invalid value map")
	ErrValueMapNotFound = errors.Base("value map not found")
)

Functions

func WithValueMap

func WithValueMap(ctx context.Context, valuemap ValueMap) context.Context

Create a context with the value map using a default key.

func WithValueMapWithKey

func WithValueMapWithKey(ctx context.Context, key string, valuemap ValueMap) context.Context

Create a context with the value map using the specified key.

Types

type ValueMap

type ValueMap interface {
	Get(string) (key any, ok bool)
	Set(key string, value any)
}

A map-based storage structure to pass multiple values via contexts rather than many invocations of `context.WithValue` and their respective copy operations.

The main caveat with this approach is that as contexts are copied by the various `With` functions we have no means of passing changes to child contexts once the context with the value map is copied.

This is not the main aim of this type, so such functionality should not be considered. The main usage is to provide a means of passing a lot of values to some top-level context in order to avoid a lot of `WithValue` calls and a somewhat slow lookup.

func GetValueMap

func GetValueMap(ctx context.Context) (ValueMap, error)

Get the value map (if any) from the context.

Returns nil if there is no value map.

func GetValueMapWithKey

func GetValueMapWithKey(ctx context.Context, key string) (ValueMap, error)

Get the value map (if any) from the context with the specified value key.

func NewValueMap

func NewValueMap() ValueMap

Create a new value map with no data.

type ValueMapKey

type ValueMapKey string

ValueMap key type for `WithValue`.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL