Context详解
00:00
context.Context与取消传播
1. 基本用法
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
result, err := fetchWithTimeout(ctx, url)
2. 传播取消
func handler(ctx context.Context) {
go func() {
select {
case <-ctx.Done():
log.Println("Cancelled:", ctx.Err())
case <-time.After(10 * time.Second):
log.Println("Done")
}
}()
}