package main import ( "fmt" "sync" ) func print(idx int, wg *sync.WaitGroup) { fmt.Printf("index %d: \n", 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
原文地址:https://www.cnblogs.com/zengyjun/p/10062471.html
时间: 2024-10-17 02:50:58