Context是Go中管理协程生命周期的核心接口,通过Deadline、Done、Err和Value方法传递取消信号、超时及请求范围的值;使用context.Background或context.TODO作为根,可派生带取消功能的子context,调用cancel函数通知所有相关goroutine。

在Go语言开发中,context 是控制协程生命周期的核心机制,尤其在处理超时和取消操作时非常关键。它能让多个goroutine之间同步请求状态,比如取消信号或截止时间。合理使用 context 可以避免资源泄漏、提升服务响应性。
什么是Context?
Context 是一个接口,定义了四个核心方法:Deadline、Done、Err 和 Value。它能传递请求范围的值、取消信号和超时信息。多个goroutine可以共享同一个 context,一旦触发取消,所有相关操作都能收到通知。
创建 context 通常从 context.Background() 或 context.TODO() 开始,作为根context,然后基于它派生出带有取消或超时功能的子context。
主动取消操作
使用 context.WithCancel 可以创建一个可手动取消的 context。调用 cancel 函数后,所有监听该 context 的 Done 通道的 goroutine 都会收到信号。
立即学习“go语言免费学习笔记(深入)”;
示例代码:
ctx, cancel := context.WithCancel(context.Background())
defer cancel() // 确保释放资源
go func() {
time.Sleep(2 * time.Second)
cancel() // 2秒后触发取消
}()
select {
case <-ctx.Done():
fmt.Println("收到取消信号:", ctx.Err())
case <-time.After(5 * time.Second):
fmt.Println("正常完成")
}
上面代码中,cancel 被调用后,ctx.Done() 通道关闭,select 会立即执行 ctx.Done() 分支,输出取消原因(context canceled)。
设置超时时间
实际开发中,更多使用 context.WithTimeout 或 context.WithDeadline 来防止请求长时间阻塞。
WithTimeout 设置从当前时间起的持续时间后自动取消;WithDeadline 则指定一个具体的时间点取消。
超时控制示例:
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
result := make(chan string, 1)
go func() {
// 模拟耗时操作
time.Sleep(5 * time.Second)
result <- "操作完成"
}()
select {
case res := <-result:
fmt.Println(res)
case <-ctx.Done():
fmt.Println("超时了:", ctx.Err())
}
由于操作耗时5秒,超过context的3秒限制,最终会走 ctx.Done() 分支,输出 "超时了: context deadline exceeded"。
在HTTP请求中的应用
net/http 包原生支持 context。发送HTTP请求时,可以把带超时的 context 传入,避免请求卡住。
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
req, _ := http.NewRequest("GET", "https://httpbin.org/delay/3", nil)
req = req.WithContext(ctx) // 旧版本用此方式
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("请求失败:", err)
return
}
defer resp.Body.Close()
fmt.Println("状态码:", resp.StatusCode)
目标接口延迟3秒返回,但客户端只等2秒,因此请求会被取消,返回超时错误。
注意:Go 1.13 后推荐直接在 NewRequestWithContext 中传入 context,更简洁安全。
传递请求数据
context 还可用于传递请求级别的元数据,比如用户ID、trace ID等,但不应传递可选参数或用于控制逻辑。
ctx := context.WithValue(context.Background(), "userID", "12345")
go func(ctx context.Context) {
if uid, ok := ctx.Value("userID").(string); ok {
fmt.Println("用户ID:", uid)
}
}(ctx)
注意:Value 查找是链式向上查找,类型断言必须做,避免 panic。
基本上就这些。掌握 context 的取消与超时机制,是编写健壮并发程序的基础。合理使用,可以让系统更可控、更高效。










