Hello!
I’d like to know why, depending on the repetitions, sync.WaitGroup won’t do everything it’s supposed to do.
Here’s an example:
package main
import (
"fmt"
"strconv"
"sync"
)
type person struct {
name string
}
const amount = 20000
func withoutWaitGroup() {
var people []person
for i := 0; i < amount; i++ {
people = append(people, person{name: "a"+strconv.Itoa(i)})
}
fmt.Printf("sequential: length should be %d, got %d.", amount, len(people))
}
func main() {
withoutWaitGroup()
// it prints -> sequential: length should be 20000, got 20000.
}
Using the same idea, but with sync.WaitGroup instead, it’ll show:
package main
import (
"fmt"
"strconv"
"sync"
)
type person struct {
name string
}
const amount = 20000
func withWaitGroup() {
var people []person
var wg sync.WaitGroup
for i := 0; i < amount; i++ {
wg.Add(1)
go func(count int) {
people = append(people, person{name: "a"+strconv.Itoa(count)})
wg.Done()
}(i)
}
wg.Wait()
fmt.Printf("sync.WaitGroup: length should be %d, got %d.", amount, len(people))
}
func main() {
withWaitGroup()
// it prints -> sync.WaitGroup: length should be 20000, got 513.
}
If I change, from 20000 repetitions, to, say, 1000, it works just fine.
Is there a reason for such behavior? Is it a known bug or am I missing something?
Here’s the code: https://play.golang.org/p/4ZATD_Z2Ui