This is a creation in Article, where the information may have evolved or changed.
Worker Pools
PackageMainImport "FMT"Import "Time"//Use Goroutine to open a thread pool of size 3//1 channel for the execution of communication, 1 to save the results//worker createdfuncWorker (IDint, Jobs <-Chan int, resultsChan<-int) { forJ: =RangeJobs {FMT. Println ("Worker"Id"Processing Job", j) time. Sleep (time. Second) Results <-J *2}}funcMain () {//Create channelJobs: = Make(Chan int, -) Results: = Make(Chan int, -)//3 worker as a pool forW: =1; W <=3; w++ {GoWorker (W, Jobs, results)}//Send 9 jobs, then close forJ: =1; J <=9; J + + {jobs <-J}Close(jobs)///FINAL collection of results forA: =1; A <=9; a++ {<-results}}
Output:
Worker 1 Processing Job 1
Worker 2 Processing Job 2
Worker 3 Processing Job 3
Worker 1 Processing Job 4
Worker 2 Processing Job 5
Worker 3 Processing Job 6
Worker 1 Processing Job 7
Worker 2 Processing Job 8
Worker 3 Processing Job 9
Real 0m3.149s