Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Merge ¶ added in v0.217.0
Merge combines multiple contexts into one. The merged context will include all values from the source contexts. If any source context is cancelled, the merged context will be cancelled. If multiple source contexts have deadlines, the nearest deadline will be used for the merged context. The second function argument must be deferred to prevent goroutine leaks.
Example ¶
package main import ( "context" "time" "go.llib.dev/frameless/pkg/contextkit" ) func main() { type key string var ( ctx1 = context.WithValue(context.Background(), key("foo"), 42) ctx2 = context.WithValue(context.Background(), key("bar"), 128) ctx3, cancel3 = context.WithTimeout(context.Background(), time.Hour) ) defer cancel3() ctx, cancel := contextkit.Merge(ctx1, ctx2, ctx3) defer cancel() _ = ctx.Value(key("foo")) // 42 _ = ctx.Value(key("bar")) // 128 _, _ = ctx.Deadline() // Deadline = deadline time value; OK=true }
Types ¶
type ValueHandler ¶ added in v0.217.0
type ValueHandler[Key ~struct{}, Value any] struct{}
Example ¶
package main import ( "context" "go.llib.dev/frameless/pkg/contextkit" ) func main() { type MyContextKey struct{} type MyValueType string vic := contextkit.ValueHandler[MyContextKey, MyValueType]{} var ctx = context.Background() // empty context v, ok := vic.Lookup(ctx) _, _ = v, ok // "", false ctx = vic.ContextWith(ctx, "Hello, world!") // context with value v, ok = vic.Lookup(ctx) _, _ = v, ok // "Hello, world!", true }
func (ValueHandler[Key, Value]) ContextWith ¶ added in v0.217.0
func (h ValueHandler[Key, Value]) ContextWith(ctx context.Context, v Value) context.Context
Click to show internal directories.
Click to hide internal directories.