context

package
v0.0.0-...-986f64f Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2023 License: BSD-3-Clause Imports: 2 Imported by: 0

Documentation

Overview

本包提供了增强的context

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithValue

func WithValue(parent context.Context, m Map) context.Context

WithValue返回带有m的所有key,value的context.Context

Example (Map)
m1 := make(map[interface{}]interface{})
m1["a"] = "b"
ctx := WithValue(context.Background(), GoMap(m1))
fmt.Println(ctx.Value("a"))
Output:

b
Example (SyncMap)
var m1 sync.Map
m1.Store("a", "b")
ctx := WithValue(context.Background(), &m1)
fmt.Println(ctx.Value("a"))
Output:

b

Types

type GoMap

type GoMap map[interface{}]interface{}

为map[interface{}]interface{}实现Map

func (GoMap) Load

func (m GoMap) Load(key interface{}) (value interface{}, ok bool)

实现Map

type Map

type Map interface {
	//获取key对应的value,ok表示是否获取成功
	Load(key interface{}) (value interface{}, ok bool)
}

map的抽象

&sync.Map和GoMap(map[interface{}]interface{})实现了本接口

Jump to

Keyboard shortcuts

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