Code Room
Code reviewMediumcr-g231
Subject Concurrency bugsLevel Mid–Senior~25 minCommon in Concurrency interviewsIndustries Software development

Question

Review this Go code (built with Go 1.21) that launches per-shard workers.

There are two distinct concurrency bugs. Identify both.

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.

Talk through your review
Code to reviewgo
func fanOut(shards []*Shard, results map[int]int) {    var wg sync.WaitGroup    for i := 0; i < len(shards); i++ {        wg.Add(1)        go func() {            defer wg.Done()            n := shards[i].Count()      // (1)            results[i] = n              // (2)        }()    }    wg.Wait()}
Run or narrate your approach, then ask the coach.