Writing Go iterators in a pull style, feedback asked

I’m relatively new to Go so I came late for the 1.23 controversy on function iterators but now that I’ve been using it regularly from my projects i started played with them and I understand they’re easy to use but also combersome to write. So I started theorizing that maybe it would be easier to write them if they were designed as pull based iterators (like Python’s, that implement a function next() that returns the next element each time it’s called and raises an exception when it’s finished), so I made this: itertools package - github.com/n-mou/yagul/itertools - Go Packages

Basicly, Go allows to turn a regular iterator in a pull based one by using the functions iter.Pull or iter.Pull2, which return next() and stop() functions. next() returns the next element each time is called and a boolean that signals if the values returned are valid or not (eg: when the iterator has finished) and the stop() is a cleanup function that must be called after using the iterator or breaking the loop without exhausting it. What this library does is implement the way back (define 2 interfaces and a transforming function that take any type that implements those functions and return an iter.Seq or iter.Seq2 for be used in the “for i := range” block.

I need feedback from gophers. What do you think? Does anyone also finds this useful or more convenient than the regular way of writing iterators?

1 Like

To be honest, I don’t see what you define as convenient. For your own projects, it’s normal to develop tools that are convenient for you. Of course, it should be how convenient and how to come, and the charm of golang is like this, without too many constraints.

1 Like