Skip to content

Puzzles

Why does the following code not work -

package main
import "time"
func worker(input <-chan int, result chan<- int) {
println("Entered worker")
for val := range input {
time.Sleep(time.Second)
result <- val * 2
}
println("Exited worker")
}
func main() {
input := make(chan int)
results := make(chan int)
for range 2 {
go worker(input, results)
}
for i := range 5 {
input <- i
}
close(input)
for range 5 {
println(<-results)
}
}