Channel close races with send
Review this Go worker pool that closes a results channel.
What's the race in how the channel is closed?
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 25 min
Mark a line and say what kind of problem it is.0 findings
1func process(jobs []Job) []Result {
2 out := make(chan Result, len(jobs))
3 var wg sync.WaitGroup
4 for _, j := range jobs {
5 wg.Add(1)
6 go func(j Job) {
7 defer wg.Done()
8 out <- run(j)
9 }(j)
10 }
11 go func() { close(out) }() // (1)
12 var rs []Result
13 for r := range out { // (2)
14 rs = append(rs, r)
15 }
16 wg.Wait()
17 return rs
18}
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.