All goroutines are asleep error

Get deadlock errror after all results have been printed out. Not sure how to fix it. Think its an issues from the way the channel was closed.
https://play.golang.org/p/MnDTp-ETigl

2 Likes

Since c hasn’t been closed yet, the for … range will wait for an 8th value, and a 9th, until c gets closed. But You only close c after the loop has been left, which won’t happen as c isn’t closed.

3 Likes

The things that im not sure where to close the channel. Closed it after the loop for sending, and i get an error for a closed channel

2 Likes

As you know how many items you expect, just close after receiving the 7th.

2 Likes

Tried closing it after ive gotten all the results from the loop. guess i didnt implement it properly
https://play.golang.org/p/B0GC0DRdkWf

2 Likes

i will never be greater then len(x), as it iterates from 0 to 6, len(x) though is 7.

Also you are trying to close after “spawning” 7 go routines, you need to close it after you have received 7 values.

2 Likes

https://play.golang.org/p/kxX0--v4Hl3
I have an issue in closing the channel after it has completed the task. How can i close it after it has gotten all the results?
This is what i did
https://play.golang.org/p/kxX0--v4Hl3

2 Likes

Count them. Your last version has the same problem as your first.

3 Likes

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