4 ms·
Yes, iterators are typically a hard[1] problem in Go. I think this essay[2] describes perhaps the closest lazy eval iterator pattern in Go, which is much more c
by iib 5y ago
Yes, iterators are typically a hard[1] problem in Go. I think this essay[2] describes perhaps the closest lazy eval iterator pattern in Go, which is much more complex to write than languages like Python.
[1] https://ewencp.org/blog/golang-iterators/index.html https://ewencp.org/blog/golang-iterators/index.html [2013]
[2] www.catb.org/~esr/reposurgeon/GoNotes.html [2020]
- johnmaguire 5y agoClickable: http://www.catb.org/~esr/reposurgeon/GoNotes.html http://www.catb.org/~esr/reposurgeon/GoNotes.html
- throwaway894345 5y agoI don’t understand what’s hard about iterators in Go after reading that article. I’ve written a lot of Python and Go (15 and 10 years respectively) as well as a bit of Rust and while iterators are certainly simpler in Python and Rust since Go lacks generics, most of the “challenges” presented in the article seem petty (complaints about for loops). The easiest way to write an iterator in Go is the closure/generator method and you can use it in a for loop like so: for x := next(); x != nil; x = next() { ... } Not as nice as a for/range but not a real problem. With generics we can have libraries that operate on iterators like map, filter, reduce although I also think people make way too big a deal about a tiny bit of for loop boilerplate (there are legitimate reasons for generics but I don’t think small amounts of boilerplate are among them).
- psanford 5y agoThere's nothing hard about writing iterators in go.