07/02/2020 - GO
If you are running multiple goroutines and want to wait for all to finish before proceeding to next step, you can use WaitGroup.
package main
import (
"fmt"
"sync"
"time"
)
var wg sync.WaitGroup
func main() {
teams := map[string]int{
"Fenerbahce": 2,
"Besiktas": 3,
"Galatasaray": 0,
"Trabzonspor": 4,
"Altay": 1,
"Bolu": 10,
}
wg.Add(len(teams))
for k, v := range teams {
go greet(k, v)
}
wg.Wait()
}
func greet(team string, delay int) {
defer wg.Done()
time.Sleep(time.Duration(delay) * time.Second)
fmt.Println(team)
}
Galatasaray
Altay
Fenerbahce
Besiktas
Trabzonspor
Bolu