package main
import (
"fmt"
"sync"
"time"
"sync/atomic"
)
func main() {
wg := sync.WaitGroup{}
var i int32
for i =0; i <20; i++ {
wg.Add(1)
tmp := atomic.LoadInt32(&i)
go func() {
defer wg.Done()
fmt.Println("i :", tmp)
}()
}
wg.Wait()
var wg1 sync.WaitGroup
for i :=0; i <5; i++ {
wg1.Add(1)
go func(n int) {
defer wg1.Done()
EchoNumber(n)
}(i)
}
wg1.Wait()
}
func EchoNumber(i int){
time.Sleep(10)
fmt.Println(i)
}
有疑问加站长微信联系(非本文作者)