funcworker(jobChan<-chanJob){forjob:=rangejobChan{process(job)}}// make a channel with a capacity of 1024.jobChan:=make(chanJob,1024)// start the workergoworker(jobChan)// enqueue a jobjobChan<-job
packagemainimport("fmt""time")funcworker(jobChan<-chanint){forjob:=rangejobChan{fmt.Println("current job:",job)time.Sleep(3*time.Second)fmt.Println("finished job:",job)}}funcmain(){// make a channel with a capacity of 1.jobChan:=make(chanint,1)// start the workergoworker(jobChan)// enqueue a jobfmt.Println("enqueue the job 1")jobChan<-1fmt.Println("enqueue the job 2")jobChan<-2fmt.Println("enqueue the job 3")jobChan<-3fmt.Println("waiting the jobs")time.Sleep(10*time.Second)}