box

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2024 License: Apache-2.0 Imports: 2 Imported by: 2

README

boxCtx

使用过多的context.WithValue会导致context 层数过多,从而导致内存占用过多和降低效率,采用此工具可以有效的降低context的层级

用法

boxCtx 也是 context.Context 所以context中的其他方法也适用,例如:

xCtx := context.WithValue(bCtx,"x","x")

或者

c, f := context.WithCancel(bCtx)

可以使用context.Value直接获取,box 也提供了一个范型的From方法,用于方便获取

x , ok := box.From[string](bCtx, "x")

box.From方法兼容context.Value

最佳实践

尽量将boxCtx放置在context树的最上层

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func From

func From[V any](ctx context.Context, key any) (v V, ok bool)

func MustFrom added in v0.0.2

func MustFrom[V any](ctx context.Context, key any) V

Types

type BoxCtx added in v0.0.2

type BoxCtx struct {
	context.Context
	// contains filtered or unexported fields
}

func Background added in v0.0.4

func Background() *BoxCtx

func WithContext added in v0.0.4

func WithContext(parent context.Context) *BoxCtx

func (*BoxCtx) Put added in v0.0.2

func (p *BoxCtx) Put(k, v any) *BoxCtx

func (BoxCtx) String added in v0.0.2

func (BoxCtx) String() string

func (*BoxCtx) Value added in v0.0.2

func (p *BoxCtx) Value(key any) any

type BoxKey added in v0.0.2

type BoxKey struct {
	Key int64
}

func RndKey added in v0.0.3

func RndKey() BoxKey

Jump to

Keyboard shortcuts

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