Handling two channels with read and write

Hi ,

I’m trying to do something for understanding and facing the problem like “all goroutines are asleep - deadlock!”. I’m having go routine to read and write too. Then why I’m getting error?

package main

func main() {

func processData() {
ch :=make(chan int , 10)
result :=make(chan int , 10)
for i:=0;i<10; i++{
go executeTask(ch, result)
for i:=0; i<100; i++{
ch <- i

for a:=0;a <10 ; a++{
	<- result
println("test main")


func executeTask(ints chan int , result chan<- int) {
for r:= range ints{
result <- 1

println(“execute tasks”)

Because you are trying to send 100 items before reading any responses. Those 100 items need to be stored somewhere.

The 10 executor goroutines reads 10 items, handles them, and writes the result to the result buffer. The result buffer is now full, because you’re not reading it yet.

Then another ten items get processed and all the executor routines all block on sending the result because the result channel is full. That’s 20 items so far.

The ch channel then buffers 10 of them. You’ve now sent 30 items.

At this point nothing can proceed – the executors can’t send results because noone is reading the results, and no new items can be sent because the executors aren’t reading any, so the system deadlocks.

1 Like

Thank you so much Jakob for the explanation

1 Like

Please mark the question as solved/answered.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.