package main
import (
"fmt"
"sync"
"time"
)
func job(worker int)int{
time.Sleep(time.Millisecond*500)
return worker
}
func main(){
start:=time.Now()
wg:=sync.WaitGroup{}
reciveChan :=make(chan int)
for i:=0;i<5;i++{
wg.Add(1)
go func(par int) {
reciveChan <- job(par)
wg.Done()
}(i)
}
go func() {
defer close(reciveChan)
wg.Wait()
}()
for worker :=range(reciveChan){
fmt.Println(worker)
}
end:=time.Since(start)
fmt.Println("這批任務總共耗時為:",end.String())
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結
:kissing_closed_eyes:
我愛小硯 乀(ˉεˉ乀)