从零学习 Go 语言(32):理解 Go 语言中的 Context

2020-07-08 00:00:00 监控 接收 结束 通道 监控器

1. 什么是 Context?

在 Go 1.7 版本之前,context 还是非编制的,它存在于 golang.org/x/net/contex 包中。

后来,Golang 团队发现 context 还挺好用的,就把 context 收编了,在 Go 1.7 版本正式纳入了标准库。

Context,也叫上下文,它的接口定义如下

type Context interface {
    Deadline() (deadline time.Time, ok bool)
    Done() <-chan struct{}
    Err() error
    Value(key interface{}) interface{}
}

相关文章