Yep, I know that the goroutine is always running, but it won't push on
the channel if not "asked for" with x = <-id0.
I can solve it easily either without explicitly closing it or
signaling to exit with another channel, but I still don't understand
when the goroutine is pushing the value
4 | Using channels to emulate Python generators kind of works, but they introduce concurrency where none is needed, and it adds more complication than's probably needed. Here, just keeping the state explicitly is easier to understand, shorter, and almost certainly more efficient. It makes all your questions about buffer sizes and garbage collection moot.
|
Not really true. The goroutine has generated the value and is blocked while waiting to _complete_ sending it on the channel. This assumes an unbuffered channel. The assignment reads the value and the goroutine unblocks to generate the next one. The goroutine is __in the processing of sending__ when the channel is closed, so the send aborts and the goroutine panics.
What it really appears, to me, that you want is a often called a "generator function". If so, perhaps this will help:
I rather like one of the examples in