07/02/2020 - GO
Birden fazla goroutine çalıştırıyorsanız ve bir sonraki adıma geçmeden önce hepsinin bitmesini beklemek istiyorsanız, WaitGroup kullanabilirsiniz.
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