A fixed number of goroutine first start, each goroutine in obtaining data from a channel, if the channel is empty, block waiting there
channel passed a Car type, which is responsible for a specific task to do
is channel that transport channel , car pass on this passage of such a car to a car on the handling of the car to work without a car that came in that these workers waiting
main Package Import ( " log " " Sync " " Time " ) type struct {Pool workerNum int Road Chan * Car WG Sync .WaitGroup } // initialize the object FUNC newpool (Wn of int ) * Pool { return & Pool {workerNum: Wn of, Road: the make (Chan * Car)} } // to add specific tasks channel FUNC (P * Pool) AddCar (F * Car) { p.road <- F } //goroutine去工作 func (p *Pool) work(workId int) { for f := range p.road { log.Println("workId:", workId, "start") f.do() log.Println("workId:", workId, "done") } p.wg.Done() } //创建goroutine等着接工作 func (p *Pool) Run() { for i := 0; i < p.workerNum; i++ { go p.work(i) p.wg.Add(1) } P.wg.Wait () } FUNC (P * Pool) colse () { Close (p.road) } FUNC main () { the pool: = newpool ( . 5 ) Go FUNC () { // simulation done 10 thing for I: = 0 ; I < 10 ; I ++ { CAR: = Car { param: I, } pool.AddCar ( & CAR) } pool.colse () } () pool.Run () } / * DETAILED do things to pass through this * / type struct {Car param int } FUNC (C * Car) do () {error log.Println ( Time .Now (), c.param) return nil }