Go语言Context包源码学习
0媒介context包作为利用go进行server端开发的重要工具,其源码只有791行,不包罗注释的话预计在500行左右,非常值得我们去深入探究学习,于是在本篇条记中我们一起来观察源码的实现,知其然更要知其所以然。(当前利用go版本为1.22.2)
1核心数据布局
团体的接口实现和布局体embed图
https://img2024.cnblogs.com/blog/3542244/202410/3542244-20241022170019786-1846604566.png
1.1Context接口
https://img2024.cnblogs.com/blog/3542244/202410/3542244-20241022170033427-397052503.png
context接口定义了四个方法:
[*]Deadline方法返回context是否为timerctx,以及它的结束时间
[*]Done方法返回该ctx的done channel
[*]Err方法返回该ctx被取消的原因
[*]Value方法返回key对应的value
2emptyCtx
先来观察源码
type emptyCtx struct{}func (emptyCtx) Deadline() (deadline time.Time, ok bool) { return}func (emptyCtx) Done()
页:
[1]