I am playing with channels and don't quite understand why the code I wrote becomes unreachable at this point and also a deadlock arises. Shouldn't the select take the value from the quit channel and then exit from the infinite for loop?
func run(jobs []Job, receive, quit chan int) {
for _, job := range jobs {
go func(job Job) {
receive <- longCalculation(job)
}(job)
}
quit <- -1 // write to the channel to signal end of work
}
func main() {
rand.Seed(time.Now().UnixNano())
jobs := makeJobs()
receive := make(chan int)
quit := make(chan int)
run(jobs, receive, quit)
var sum int
for {
select {
case data := <-receive:
sum += data
case <-quit: // take the last value indicating end of work
fmt.Println("Exiting for loop ...", quit)
return
}
}
fmt.Print(sum) -> this line says unreachable code
}
Change it into
It's because the function stop immediately when you return