Skip to content

Instantly share code, notes, and snippets.

@jayjayswal
Created October 30, 2019 10:27
Show Gist options
  • Save jayjayswal/c05b99cbc2df73cffde04aca0de5197d to your computer and use it in GitHub Desktop.
Save jayjayswal/c05b99cbc2df73cffde04aca0de5197d to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"time"
)
func worker(id int, jobs <-chan int, results chan<- int) {
for j := range jobs {
fmt.Println("worker", id, "processing job", j)
time.Sleep(time.Second)
results <- j * 2
}
}
func main() {
job := make(chan int, 10)
result := make(chan int, 10)
for w := 1; w <= 2; w++ {
go worker(w, job, result)
}
for j := 1; j <= 9; j++ {
job <- j
}
close(job)
for a := 1; a <= 9; a++ {
<-result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment