waitgroups.go
- package main
-
- import (
- "fmt"
- "sync"
- "time"
- )
-
- func w(id int) {
- fmt.Printf("Worker %d starting \n", id)
- time.Sleep(time.Second)
- fmt.Printf("Worker %d done \n", id)
- }
- func main() {
-
- var wg sync.WaitGroup
-
- for i := 1; i <= 5; i++ {
- wg.Add(i)
- i := i
- go func() {
- defer wg.Done()
- w(i)
- }()
- }
-
- wg.Wait()
- }