Aggregator concurrent close
Review this Go streaming aggregator. Producers send partial results; a coordinator closes the channel on the first error.
What a strong answer looks like
Separate real bugs from style. Rank issues by severity, point at the root cause rather than the symptom, and suggest a concrete fix, specific and kind.
0:00 of about 26 min
Mark a line and say what kind of problem it is.0 findings
1func run(items []Item) error {
2 results := make(chan int)
3 var wg sync.WaitGroup
4 for _, it := range items {
5 wg.Add(1)
6 go func(it Item) {
7 defer wg.Done()
8 v, err := process(it)
9 if err != nil {
10 close(results) // signal failure
11 return
12 }
13 results <- v
14 }(it)
15 }
16 go func() { wg.Wait(); close(results) }()
17 total := 0
18 for v := range results {
19 total += v
20 }
21 return nil
22}
Which questions mattered is sealed until you submit. Telling you now would just be handing over the edge cases.
Run or narrate your approach, then ask the coach.