so i have this simple go routine code
func jobX(wg *sync.WaitGroup) {
defer wg.Done()
for i := 0; i < 5; i++ {
fmt.Println("routine X: ", i)
if i == 2 {
time.Sleep(3 * time.Second)
}
}
}
func jobY() {
for i := 0; i < 5; i++ {
fmt.Println("routine Y: ", i)
}
}
func main() {
var wg sync.WaitGroup
fmt.Println("starting go routine...")
wg.Add(1)
go jobX(&wg)
jobY()
fmt.Println("end")
wg.Wait()
}
when i run the program, this is the result:
starting go routine...
routine Y: 0
routine Y: 1
routine Y: 2
routine Y: 3
routine Y: 4
end
routine X: 0
routine X: 1
routine X: 2
routine X: 3
routine X: 4
but this is what i expect it to be:
starting go routine...
routine X: 0
routine X: 1
routine X: 2
routine Y: 0
routine Y: 1
routine Y: 2
routine Y: 3
routine Y: 4
routine X: 3
routine X: 4
end
i cant figure it out how should i change my code to get this result.
You need a second increment of your
waitGroupfor yourY, and then you also need to move theWaitup before the "end":...and make the corresponding changes to your
jobYfunc so it can callwg.Done()likejobXis.