面试题答案
一键面试1. Context在多个Goroutine之间有效传递的方式
在Go语言中,通常将Context
作为参数在函数调用链中传递,从而将其传递到各个需要它的Goroutine
。一般在启动新的Goroutine
时,将Context
作为参数传递进去。
2. 这样做的好处
- 控制并发:可以通过
Context
统一控制多个Goroutine
的生命周期,例如在程序退出、超时等情况下,能够同时通知所有相关的Goroutine
进行清理并退出。 - 资源管理:方便进行资源的清理工作,比如关闭数据库连接、文件句柄等,避免资源泄漏。
- 超时处理:可以设置操作的超时时间,防止
Goroutine
长时间阻塞导致程序响应变慢。
3. 代码示例
package main
import (
"context"
"fmt"
"time"
)
func worker(ctx context.Context, id int) {
for {
select {
case <-ctx.Done():
fmt.Printf("Worker %d received termination signal\n", id)
return
default:
fmt.Printf("Worker %d is working\n", id)
time.Sleep(1 * time.Second)
}
}
}
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
for i := 1; i <= 3; i++ {
go worker(ctx, i)
}
time.Sleep(5 * time.Second)
fmt.Println("Main function exiting")
}
在上述代码中:
context.WithTimeout
创建了一个带有超时的Context
,超时时间为3秒。cancel
函数用于手动取消Context
,这里通过defer
确保在函数结束时调用,以释放相关资源。worker
函数接收Context
作为参数,通过select
监听ctx.Done()
通道,当接收到信号时,代表Context
被取消,Goroutine
执行清理并退出。