Fatal error: all goroutines are asleep when reading from channel on main goroutine

Getting the following error while running the below code.

fatal error: all goroutines are asleep - deadlock!

goroutine 1 [chan receive]:

Link: https://go.dev/play/p/2zey2TNZmv2

Note: I am getting this error while reading from the channel on the main goroutine. If I read using a separate go routine, then it works fine. I am curious to know why this is not wokring on main thread.

The channel is never closed, so the for range will wait for a fourth item.

I doubt it works when you use a separate go routine, it just appears to be working, as main waits for the wg, and gets a sync point there, though the receiving go routine is still waiting for data indefinitely.

1 Like

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