Why does Go (so far) not have support for generators/ iterators for custom types ?

187 views
Skip to first unread message

Serge Hulne

unread,
Aug 6, 2023, 4:05:11 AM8/6/23
to golang-nuts
Why does Go (so far) not have support for generators/ iterators for custom types ?

I wrote this workaround:

But I was wondering what lead to the idea of leaving iterators out in the early days of the design of Go. What simplification of the language was expected from leaving out iterators for custom types.


Axel Wagner

unread,
Aug 6, 2023, 5:36:02 AM8/6/23
to Serge Hulne, golang-nuts
For one, Go did not have generic types or functions, which really limits the usefulness (and abilities to write) custom iterators in Go.
Now we do have iterators, but it turns out there still are a lot of subtle Go-specific issues with how to support custom iterator types, which to be honest are too complicated to explain in an E-Mail. You can skim these discussions, to get an impression:
https://github.com/golang/go/discussions/54245 (discussion, probably the first serious attempt)
https://github.com/golang/go/discussions/56413 (discussion, follows from the previous one with a significantly simpler approach)
https://github.com/golang/go/issues/61405 (proposal, from 3 weeks ago, currently actively discussed)
If you decide to participate, I strongly recommend reading both the top-post (with the actual proposal) and the first comment (with an extensive summary of the discussion and FAQ).

It's complicated.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/1fd9e5e8-8196-4bfb-910c-89ed461c42c5n%40googlegroups.com.

Axel Wagner

unread,
Aug 6, 2023, 5:37:00 AM8/6/23
to Serge Hulne, golang-nuts
Small corrections:
1. in the first sentence, add an "early days Go"
2. In the second sentence, "now we do have *generics*", not iterators.
Reply all
Reply to author
Forward
0 new messages