Help! can some one help why this code is not working following is the code

1 Like

Because c is unbuffered a lot of go routines try to write to it and block because there is no more space in the channel.

Since you do not return from fanout before all “results” are sent to the channel, you never reach the code that actually reads from c.

1 Like

Thanks…got it …How to do it without adding Buffer.
One more thing why data is not getting pulled out of channel simultaneously . Does waitgroup will not let data to be pulled concurrently.

You need to fork out fanout(c) function in your main code. Otherwise, sync.WaitGroup is treated as part of main execution, which is why your data do not get read simultaneously.

There are many ways to approach it:

  1. Delegate the fanout(c) into an independent goroutine, main becomes the printer after delegation:
  2. fanout(c) delegates smaller tasks. main maintains its original codes.
  3. Delegate fantout(c) and a printer(c) that focuses on printing. This one needs synchronization to main.

The root cause is actually your concurrency planning. It is not clear enough with:

  1. when to delegate what task by who
1 Like

So dumb of me …i by mistake haven’t called it as goroutine…thanks for that

No worries. Better than not asking. :grin: Feel free to “mark as solved” once you found your answer.

1 Like

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