Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Wait for goroutines

  • sync
  • WaitGroup
  • Add
  • Wait
  • Done
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