I am trying to create a threadpool of workers that do jobs.
This seems to working fine, but it if I, for instance, input 1-6. It won't print out six.
Can someone explain why and hopefully provide a fix for me?
// Golang Workerpool
func worker(id int, jobs <-chan string) {
fmt.Println("Worker", id, "initilized!")
for {
s := <- jobs
time.Sleep(2 * time.Second)
fmt.Println("Worker", id, "said:", s);
}
}
func main() {
// In order to use our pool of workers we need to send
// them work and collect their results. We make 2
// channels for this.
jobs := make(chan string)
// This starts up 3 workers, initially blocked
// because there are no jobs yet.
for w := 1; w <= 3; w++ {
go worker(w, jobs)
}
for {
reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString('
')
jobs <- text
}
}