Coroutines for Go
I&#x27;m not 100% sure this is the case, but I believe the context of this goes something like this. As Go has added generics, there are proposals to add generic data structures like a Set. Generics solve almost every problem with that, but there is one conspicuous issue that remains for a data structure: You can iterate over a slice or a map with the &quot;range&quot; keyword, and that yields special iteration behavior, but there is no practical way to do that with a general data structure, if you consider constructing an intermediate map or slice to be an insufficient solution. Go is generally performance-sensitive enough that it is.<p>The natural solution to this is some sort of iterator, as in Python or other languages. (Contra frequent accusations to the contrary, the Go community is aware of other language&#x27;s efforts.)<p>So this has opened the can of worms of trying to create an iteration standard for Go.<p>Go has something that has almost all the semantics we want right now. You can also &quot;range&quot; over a channel. This consumes one value at a time from the channel and provides it to the iteration, exactly as you&#x27;d expect, and the iteration terminates when the channel is closed. It just has one problem, which is that it involves a full goroutine and a synchronized channel send operation for each loop of the iteration. As I said in another comment, if what is being iterated on is something huge like a full web page fetch, this is actually fine, but no concurrency primitive can keep up with the efficiency of incrementing an integer, a single instruction which may literally take an amortized fraction of a cycle on a modern processor. With generics you can even relatively implement filter, map, etc. on this iterator... but adding a goroutine and synchronized commit for each such element of a pipeline is just crazy.<p>I believe the underlying question in this post is, can we use standard Go mechanisms to implement the coroutines without creating a new language construct, then use the compiler under the hood to convert it to an efficient execution? Basically, can this problem be solved with compiler optimizations rather than a new language construct? From this point of view, the payload of this article is really only that very last paragraph; the entire rest of the article is just orientation. If so, then Go can have coroutine efficiency with the standard language constructs that already exist. Perhaps some code that is using this pattern goroutine already might speed up too &quot;for free&quot;.<p>The concerns people have about this complexifying Go, the entire point of this operation is to suck the entire problem into the compiler with 0 changes to the spec. Not complexifying Go with a formal iteration standard is the entire point of this operation. If one wishes to complain, the correct complaint is the exact opposite one, that Go is not &quot;simply&quot; &quot;just&quot; implementing iterators as a first class construct just like all the other languages.<p>Also, in the interests of not posting a full new post, note that in general I shy away from the term &quot;coroutine&quot; because a coroutine is what this article describes, exactly, and nothing less. To those posting &quot;isn&#x27;t a goroutine already a coroutine?&quot;, the answer is, no, and in fact almost nothing called a coroutine by programmers nowadays actually is. The term got simplified down to where it just means thread or generator as Python uses the term, depending on the programming community you&#x27;re looking at, but in that context we don&#x27;t need to use the term &quot;coroutine&quot; that way, because we already <i>have</i> the word &quot;thread&quot; or &quot;generator&quot;. This is what &quot;real&quot; coroutines are, and while I won&#x27;t grammatically proscribe to you what you can and can not say, I will reiterate that I <i>personally</i> tend to avoid the term because the conflation between the sloppy programmer use and the more precise academic&#x2F;compiler use is just confusing in almost all cases.