4 ms·
Don't forget that the important bit in Go is readability of the code. To me the range form is much easier to reason about than this for loop.
by davenoul 3y ago
Don't forget that the important bit in Go is readability of the code. To me the range form is much easier to reason about than this for loop.
- Kamq 3y agoIf we're going for readability instead of trying to stick to the case presented above, I'd go with: for i.HasNext() { current := i.GetNext() ... } And just mutate the internal state of the struct. Probably throw it behind some sort of interface like: type Iterator[T any] interface { // feel free to strip out the generic HasNext() bool // if you really only have one type GetNext() T // you do this with } I think this still loses to ranging over a function, but I still think it should be compared to what you can do with the current language as opposed to what was originally posted.