🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
~~~ package main import ( "math/rand" "fmt" ) type Job struct { Number int Id int } type Result struct { job *Job sum int } func calc(job *Job, result chan *Result) { var sum int number := job.Number for number != 0 { tmp := number % 10 sum += tmp number /= 10 } r := &Result{ job: job, sum: sum, } result <- r } func Worker(jobChan chan *Job, resultChan chan *Result) { for job := range jobChan { calc(job, resultChan) } } func startWorkerPool(num int, jobChan chan *Job, resultChan chan *Result) { for i := 0; i < num; i++ { go Worker(jobChan, resultChan) } } func printResult(resultChan chan *Result) { for result := range resultChan { fmt.Printf("job id:%v number:%v result:%d\n", result.job.Id, result.job.Number, result.sum) } } //工作池 func main() { jobChan := make(chan *Job, 1000) resultChan := make(chan *Result, 1000) startWorkerPool(128, jobChan, resultChan) go printResult(resultChan) var id int for { id ++ number := rand.Int() job := &Job{ Id: id, Number: number, } jobChan <- job } } ~~~