contextx

package
v0.6.3 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2026 License: MIT Imports: 5 Imported by: 10 modules, 14 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Example
package main

import (
	"context"
	"fmt"

	"github.com/xoctopus/x/contextx"
)

type tCtxT struct{}

func main() {
	ctx := contextx.Carry[tCtxT]("100")(context.Background())
	fmt.Println(contextx.Must[tCtxT, string](ctx))
	fmt.Println(contextx.FromOr[tCtxT](ctx, "101"))

	ctx = context.Background()
	fmt.Println(contextx.FromOr[tCtxT](ctx, "102"))

	f := contextx.FromOrDefault[tCtxT, string]("103")
	fmt.Println(f(ctx))

	ctx = contextx.With[tCtxT](ctx, "104")
	fmt.Println(f(ctx))

}
Output:
100
100
102
103
104

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func From added in v0.3.0

func From[KT comparable, P any](ctx context.Context) (P, bool)

func FromOr added in v0.4.5

func FromOr[KT comparable, P any](ctx context.Context, or P) P

func FromOrDefault added in v0.5.6

func FromOrDefault[KT comparable, P any](v P) func(context.Context) P

func Must added in v0.3.0

func Must[KT comparable, P any](ctx context.Context) P

func With added in v0.0.21

func With[KT comparable, P any](ctx context.Context, v P) context.Context

func WithValue

func WithValue(parent context.Context, k, v any) context.Context

WithValue like context.WithValue but faster🤏

Example
var ctx context.Context

type key1 struct{}
type key2 struct{}
type key3 struct{}
type key4 struct{}

ctx = contextx.WithValue(MockContext{}, key1{}, "1")
fmt.Println(ctx)
ctx = contextx.WithValue(ctx, key2{}, "2")
fmt.Println(ctx)
ctx = contextx.WithValue(ctx, key3{}, net.IPv4(1, 1, 1, 1))
fmt.Println(ctx)
ctx = contextx.WithValue(ctx, key4{}, 4)
fmt.Println(ctx)

fmt.Println("context value key1:", ctx.Value(key1{}))
fmt.Println("context value key2:", ctx.Value(key2{}))
fmt.Println("context value key3:", ctx.Value(key3{}))
fmt.Println("context value key4:", ctx.Value(key4{}))
Output:
contextx_test.MockContext.WithValue(key:contextx_test.key1, val:1)
contextx_test.MockContext.WithValue(key:contextx_test.key1, val:1).WithValue(key:contextx_test.key2, val:2)
contextx_test.MockContext.WithValue(key:contextx_test.key1, val:1).WithValue(key:contextx_test.key2, val:2).WithValue(key:contextx_test.key3, val:1.1.1.1)
contextx_test.MockContext.WithValue(key:contextx_test.key1, val:1).WithValue(key:contextx_test.key2, val:2).WithValue(key:contextx_test.key3, val:1.1.1.1).WithValue(key:contextx_test.key4, val:<not Stringer>)
context value key1: 1
context value key2: 2
context value key3: 1.1.1.1
context value key4: 4

Types

type Carrier added in v0.2.1

type Carrier func(context.Context) context.Context

func Carry added in v0.3.0

func Carry[KT comparable, P any](v P) Carrier

func Compose added in v0.0.25

func Compose(carriers ...Carrier) Carrier
Example
c1 := contextx.NewT[string]()
c2 := contextx.NewT[int]()
c3 := contextx.NewT[fmt.Stringer]()

ctx := contextx.Compose(
	c1.Carry("1"),
	c2.Carry(2),
	c3.Carry(net.IPv4(1, 1, 1, 1)),
)(context.Background())
fmt.Println(ctx)
fmt.Println(c1.MustFrom(ctx))
fmt.Println(c2.MustFrom(ctx))
fmt.Println(c3.MustFrom(ctx))
Output:
context.Background.WithValue(key:*contextx.ctx[string], val:1).WithValue(key:*contextx.ctx[int], val:<not Stringer>).WithValue(key:*contextx.ctx[fmt.Stringer], val:1.1.1.1)
1
2
1.1.1.1

type Context added in v0.0.21

type Context[T any] interface {
	With(context.Context, T) context.Context
	From(context.Context) (T, bool)
	MustFrom(context.Context) T
	Carry(v T) Carrier
}

func NewT added in v0.0.25

func NewT[T any](options ...Option[T]) Context[T]

func NewV added in v0.0.25

func NewV[T any](v T) Context[T]

type Option added in v0.0.21

type Option[T any] func(*ctx[T])

func WithDefault added in v0.0.25

func WithDefault[T any](v T) Option[T]

func WithOnce added in v0.2.1

func WithOnce[T any]() Option[T]

type Provider added in v0.4.8

type Provider interface {
	WithContext(context.Context) context.Context
}

type Valuer added in v0.0.21

type Valuer[T any] func() T

Jump to

Keyboard shortcuts

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