标准库和 x/sync 已经把大部分并发场景覆盖了,手写 channel 编排的机会其实不多。
常用的三个
errgroup.WithContext:一组 goroutine,任意一个出错就取消全部,天然带 context 传播semaphore.Weighted:限并发,比手搓带缓冲 channel 更清晰,还支持带权sync.OnceValue:懒加载单例,比自己写 double-check 干净
一个坑
errgroup 的 SetLimit 要配合 Go 使用,直接在 goroutine 里 acquire 而不用 Go 注册,出错时 Wait 不会等它。