func main() { var wg sync.WaitGroup var count int wg.Add(500) for i := 1; i <= 500; i++ { //wg.Add(1) go func() { count++ defer wg.Done() }() } wg.Wait() fmt.Println(count) }
func main() { fmt.Println("main process start") var wg sync.WaitGroup for i := 0; i < 5; i++ { wg.Add(1) go process(i, &wg) } wg.Wait() fmt.Println("main process end") }
func worker(wg *sync.WaitGroup, i int) { time.Sleep(time.Second * 1) // print immediately // fmt.Println("worker process done", i) wg.Done() // may not print fmt.Println("worker process done", i) }
func main() { var wg sync.WaitGroup wg.Add(5) for i := 1; i <= 5; i++ { // fmt.Println(i) go worker(&wg, i) } wg.Wait() fmt.Println("main process done") }