Your concept is interesting, but there are two problems with your solution.
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.
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)
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!
We’re a place for programmers to stay up-to-date, learn new skills, and share ideas.
We’ll never post without your permission.