Junior — Senior
Organized output of numbers from multiple goroutines
livecode
Task condition
The program runs three goroutines, each printing numbers from its own array. It is required to modify the code so that the numbers are output in strict order, while the call to fmt.Println() must remain inside the loop of each goroutine.
package main
import (
"fmt"
"sync"
)
func main() {
var waitGroup sync.WaitGroup
waitGroup.Add(3)
go func() {
for _, v := range []int{1, 4, 7} {
fmt.Println(v)
}
waitGroup.Done()
}()
go func() {
for _, v := range []int{2, 5, 8} {
fmt.Println(v)
}
waitGroup.Done()
}()
go func() {
for _, v := range []int{3, 6, 9} {
fmt.Println(v)
}
waitGroup.Done()
}()
waitGroup.Wait()
}