1. channel
func main() { boolChan := make(chan bool, 100) for i := 0; i < 100; i++ { go func(v int) { println(v) boolChan <- true }(i) } for i := 0; i < 100; i++ { <-boolChan } }
2. sync.WaitGroup
func main() { wg := sync.WaitGroup{} wg.Add(10) for i := 0; i < 10; i++ { go func(v int) { fmt.Printf("Goroutine: %v, v: %v\n", GetGoroutineID(), v) wg.Done() }(i) } wg.Wait() }
func main() { wg := sync.WaitGroup{} wg.Add(10) for i := 0; i < 10; i++ { go t(i, &wg) } wg.Wait() } // use *sync.WaitGroup func t(v int, wg *sync.WaitGroup) { fmt.Printf("Goroutine: %v, v: %v\n", GetGoroutineID(), v) wg.Done() }
标签:wg,WaitGroup,10,Process,sync,int,Synchronization,func,Go From: https://www.cnblogs.com/dissipate/p/17228113.html