I am new in Go and doing some practices with the book "The little Go book" from chapter Concurrency. I can't figure out what happens by removing time.Sleep(time.Millisecond * 50)
that changes the output to only printing results for "worker 4".
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
c := make(chan int)
for i := 0; i < 5; i++ {
worker := &Worker{id: i}
go worker.process(c)
}
for {
c <- rand.Int()
time.Sleep(time.Millisecond * 50) // ???
}
}
type Worker struct {
id int
}
func (w *Worker) process(c chan int) {
for {
data := <-c
fmt.Printf("worker %d got %d
", w.id, data)
}
}