DeepSource has joined Harness. Learn more
Go logoGo/
SCC-SA6003

Range over the string directlySCC-SA6003

Major severityMajor
Performance categoryPerformance

Loop directly over the runes in a string instead of converting the string to a slice of rune and then looping over the same.

Bad practice

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.