首页 > 其他分享 >Go - context keys and values

Go - context keys and values

时间:2024-02-25 13:44:06浏览次数:26  
标签:keys WithValue ctx interface values context Go ctxKey type

IS THERE A WAY TO LIST KEYS IN CONTEXT.CONTEXT?

No there is no way to list all the keys of context.Context. Because that type is just an interface. So what does this mean?

In general a variables can hold a concrete type or an interface. A variable with an interface type does not have any concrete type informations on it. So it would makes no difference if the interface is empty (interface{}) or context.Context. Because they could be a lot of different types which are implementing that interface. The variable does not have a concrete type. It is just something abstract.

If you use reflection you could observer the fields and all the methods of the type which is set to that variable (with interface type). But the logic how the method Value(key interface{}) interface{} is implemented is not fixed. It does not have to be a map. You could also make an implementation with slices, database, an own type of an hash table, ...

So there is no general way to list all the values.

 

type ctxKey string

func main() {
    var key1 ctxKey = "k1"
    var key2 ctxKey = "k2"
    var key3 ctxKey = "k3"
    ctx := context.Background()
    ctx = context.WithValue(ctx, key1, "v1")
    ctx = context.WithValue(ctx, key2, "v2")
    ctx = context.WithValue(ctx, key3, "v3")
    fmt.Println(ctx)
    fmt.Printf("%v\n", ctx)

    for _, key := range [3]ctxKey{"k1", "k2", "k3"} {
        fmt.Printf("key: %v, value: %v\n", key, ctx.Value(key))
    }
}

 

zzh@ZZHPC:/zdata/Github/ztest$ go run main.go
context.Background.WithValue(type main.ctxKey, val v1).WithValue(type main.ctxKey, val v2).WithValue(type main.ctxKey, val v3)
context.Background.WithValue(type main.ctxKey, val v1).WithValue(type main.ctxKey, val v2).WithValue(type main.ctxKey, val v3)
key: k1, value: v1
key: k2, value: v2
key: k3, value: v3

 

标签:keys,WithValue,ctx,interface,values,context,Go,ctxKey,type
From: https://www.cnblogs.com/zhangzhihui/p/18032307

相关文章

  • 全能代码生成器,自动生成前后端代码、生成项目框架、生成JavaBean、生成数据库文档、自
    TableGo_20240224v8.4.0正式版发布,此次版本累计更新如下: 1、TableGo专属LOGO上线 2、生成数据库文档ER图新增备注+字段名的生成配置 3、生成自定义文件功能新增临时参数配置,用于使用临时数据生成自定义文件 4、新增基于Excel数据生成自定义文件,可导入Excel数据生成程序代码......
  • Go语言精进之路读书笔记第36条——使用atomic包实现伸缩性更好的并发读取
    atomic包提供了两大类原子操作接口:一类是针对整型变量的,包括有符号整型、无符号整型以及对应的指针类型;另一个类是针对自定义类型的。atomic包十分适合一些对性能十分敏感、并发量较大且读多写少的场合。如果要对一个复杂的临界区数据进行同步,那么首选依旧是sync包中的原语。36.......
  • Go语言精进之路读书笔记第35条——了解sync包的正确用法
    Go语言通过标准库的sync包提供了针对传统基于共享内存并发模型的基本同步原语。35.1sync包还是channel在下面一些场景下,我们依然需要sync包提供的低级同步原语(1)需要高性能的临界区同步机制场景(2)不想转移结构体对象所有权,但又要保证结构体内部状态数据的同步访问的场景......
  • Go-kit框架学习记录
    2、go-kit三层结构go-kit和MVC一样也有三层结构endpoint,service,transport,通过这三层结构来实现,接收一个请求,然后返回一个结果。1.TransportTransport处于该微服务的最上层,主要负责于HTTP,gRPC,thrift等相关的逻辑,负责解析请求,并且调用endpoint来处理请求2.Endpointendpoi......
  • golang 系统调用与阻塞处理
    所有在UNIX系统上运行的程序最终都会通过C系统调用来和内核打交道。用其他语言编写程序进行系统调用,方法不外乎两个:一是自己封装,二是依赖glibc、或者其他的运行库。Go语言选择了前者,把系统调用都封装到了syscall包。封装时也同样得通过汇编实现。当M一旦进入系统调用后,会......
  • go堆内存分配
    mutator申请内存是以应用视角来看问题,我需要的是某一个struct,某一个slice对应的内存,这与从操作系统中获取内存的接口(比如mmap)之间还有一个鸿沟。需要由allocator进行映射与转换,将以“块”来看待的内存与以“对象”来看待的内存进行映射。在现代CPU上,我们还要考虑内存分配......
  • golang io优化
    如果想兼顾开发效率,又能保证高并发,协程就是最好的选择。它可以在保持异步化运行机制的同时,用同步方式写代码(goroutine-per-connection),这在实现高并发的同时,缩短了开发周期,是高性能服务未来的发展方向。CPU和IO设备是不同的设备,能并行运行。合理调度程序,充分利用硬件,就能跑出......
  • golang性能优化
    性能优化流程理清待优化代码的常用逻辑与场景根据实际场景编写压测用例使用pprof或者火焰图等工具取得数据找到热点代码重点优化Profilingpprof是用于可视化和分析性能分析数据的工具。为什么pprof可以帮助我们分析Go程序性能呢?因为它可以采集程序运行时数据:比如说协程栈,这样服......
  • 2024-02-24:用go语言,给你一个 n 个点的带权无向连通图,节点编号为 0 到 n-1, 同时还有一
    2024-02-24:用go语言,给你一个n个点的带权无向连通图,节点编号为0到n-1,同时还有一个数组edges,其中edges[i]=[fromi,toi,weighti],表示在fromi和toi节点之间有一条带权无向边,最小生成树(MST)是给定图中边的一个子集,它连接了所有节点且没有环,而且这些边的权值和最......
  • go经典知识及总结
    1.无论sync.Mutex还是其衍生品都会提示不能复制,但是能够编译运行加锁后复制变量,会将锁的状态也复制,所以mu1其实是已经加锁状态,再加锁会死锁.所以此题的答案是fatalerror;typeMyMutexstruct{countintsync.Mutex}funcmain(){varmuMyMutexmu.L......