1. 利用waitgroup
import ( "log" "sync" "sync/atomic" "time" ) func main() { wg := sync.WaitGroup{} for i := 0; i < 10; i++ { wg.Add(1) go func(i int) { log.Println(i) wg.Done() }(i) } wg.Wait() }
2. 利用缓冲channel
import ( "log" "time" ) func main() { ch := make(chan struct{}, 10) var i int for i = 0; i < 10; i++ { go func(i int) { log.Println(i) ch <- struct{}{} }(i) } for j := i; j > 0; j-- { <-ch } }
3. 利用非缓冲channel
package main import ( "log" "time" ) func main() { ch := make(chan struct{}) for i := 0; i < 10; i++ { go func(i int) { log.Println(i) ch <- struct{}{} }(i) <-ch } }
4. 利用原子计数器
package main import ( "log" "time" "sync" "sync/atomic" ) func main() { var total int32 for i := 0; i < 10; i++ { go func(i int) { log.Println(i) atomic.AddInt32(&total,1) }(i) } for atomic.LoadInt32(&total) < 10 { time.Sleep(time.Microsecond) } }