Every time you execute a go statement it is passed to the scheduler. What if scheduling is delayed? wg.Add(1) is not executed and wg.Wait() is true. For example, run your code in the Go Playground.
package main
import (
"sync"
)
func main() {
n := 5
var wg sync.WaitGroup
wg.Add(n)
ch := make(chan int, n)
for i := 0; i < n; i++ {
println("Processing ", i)
go process(i, ch)
go consume(ch, &wg)
}
wg.Wait()
println("Finished the process")
}
func consume(ch chan int, wg *sync.WaitGroup) {
defer wg.Done()
println("Result ", <-ch)
}
func process(i int, ch chan int) {
ch <- (i * 5)
}
Playground: https://play.golang.org/p/dQ_lFRz2Y8a
Output:
Processing 0
Processing 1
Processing 2
Processing 3
Processing 4
Finished the process
Make sure that all the wg.Adds are run before wg.Wait. Move the println("Finished the process") to nthe correct place. For example,
package main
import (
"sync"
)
func main() {
n := 5
var wg sync.WaitGroup
wg.Add(n)
ch := make(chan int, n)
for i := 0; i < n; i++ {
println("Processing ", i)
go process(i, ch)
go consume(ch, &wg)
}
wg.Wait()
println("Finished the process")
}
func consume(ch chan int, wg *sync.WaitGroup) {
defer wg.Done()
println("Result ", <-ch)
}
func process(i int, ch chan int) {
ch <- (i * 5)
}
Playground: https://play.golang.org/p/rBp62M4_PwU
Output:
Processing 0
Processing 1
Processing 2
Processing 3
Processing 4
Result 0
Result 5
Result 10
Result 15
Result 20
Finished the process