Use range instead of for - because it is more idiomatic?

Hello everyone,

At work I’ve got this feedback: “use the range version of for, because it is more idiomatic to Go”.

Is it really the reange version of for loop idiomatic to Go ? Should not all three versions be equally idiomatic, as they are all accepted by the compiler ?

Secondly, is idiomatic a value ? Maintainability is a value which we desire, but I do not know about idiomaticy.

Any opinions on this ? Do you use only range for loops or also the regular (java like) one ?

If you want to iterate over numbers, then use C-style for, if you want to iterate over a collection of things, use for with range.

Because its idiomatic and much more maintainable.

There won’t be off-by-one when using range.

1 Like