https://play.golang.org/p/6nAfqT6bvtH following is the code
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
.
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:
- Delegate the
fanout(c)
into an independent goroutine,main
becomes the printer after delegation: https://play.golang.org/p/PAO5CRZd5Gz -
fanout(c)
delegates smaller tasks.main
maintains its original codes. - Delegate
fantout(c)
and aprinter(c)
that focuses on printing. This one needs synchronization tomain
.
The root cause is actually your concurrency planning. It is not clear enough with:
- when to delegate what task by who
So dumb of me …i by mistake haven’t called it as goroutine…thanks for that
No worries. Better than not asking. Feel free to “mark as solved” once you found your answer.
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.