Spot the bug Go
1
package main
2
3
import (
4
"fmt"
5
"sync"
6
)
7
8
func main() {9
var wg sync.WaitGroup
10
processed := 0
11
for i := 0; i < 500; i++ {12
wg.Add(1)
13
go func() {14
defer wg.Done()
15
processed++
16
}()
17
}
18
wg.Wait()
19
fmt.Println(processed) // expected: 500, actual: rarely 500 (!)
20
}