- sync
- WaitGroup
- Add
- Wait
- Done
Wait for goroutines
examples/wait-for-goroutines/wait_for_goroutines.go
package main import ( "fmt" "sync" ) func count(n int, name string) { for i := 1; i <= n; i++ { fmt.Printf("%v %v\n", name, i) } } func main() { fmt.Println("Start") var wg sync.WaitGroup wg.Add(1) go func() { count(5, "Apple") wg.Done() }() wg.Wait() fmt.Println("End") }
Start Apple 1 Apple 2 Apple 3 Apple 4 Apple 5 End