re: Custom range iterators in Go VIEW POST


Your concept is interesting, but there are two problems with your solution.

  1. The function takes a integer range. This function would panic at runtime if used wrong. There are no compilation checks. Also the function handles each parameter differently, despite the fact that its one integer range. I don't think that's idiomatic.

  2. I think the classic

for i := 0; i < 10; i++

is actually easier to read. Nearly every dev will understand this at first glance (even if it's not as clean as other solutions). On the other hand

for i := range IntRange(10, 20, 2)

is ambiguous for me. And it doesn't help if I look at the function signature.

I think I'm sticking with the classic solution.

I agree it looks unconventional and does not read well.

A more readable version would be

for i := range ChanOfInts(10, 20, 2)

or shorter

for i := range OfInts(10, 20, 2)

but still, it looks ugly

overall I think the best solution is to use a classic for loop you cannot be more explicit than that and you do not sacrifice clarity at all. By the was the use of go-iter or iterators is even more ugly and un-necessary. In short KISS!

code of conduct - report abuse