package main import ( "fmt" "sync" ) func print(idx int, wg *sync.WaitGroup) { fmt.Printf("index %d: ", idx) wg.Done() } func main() { var wg sync.WaitGroup for i := 0; i < 10; i++ { wg.Add(1) go print(i, &wg) } wg.Wait() }
运行结果: index 3 index 7 index 9 index 1 index 2 index 4 index 0 index 5 index 6 index 8