Loop directly over the runes in a string instead of converting the string to a
slice of rune
and then looping over the same.
for _, r := range []rune(s) {}
for _, r := range s {}
Both bad and recommended practices yield the same values, but the recommended
practice will be comparatively faster as it avoids unnecessary memory
allocations. If you are interested in the indices, ranging over a string and a
slice of rune
will yield different indices. The former yields byte
offsets,
while the latter yields indices in the slice of rune
.