What is the difference between a rune and a byte, and how does Go handle UTF-8 strings when iterating over them?
Last updated
byteis an alias foruint8(one raw byte).runeis an alias forint32(one Unicode code point, which may span several UTF-8 bytes).Strings are UTF-8 byte sequences:
s[i]returns a byte andlen(s)counts bytes, not characters.for rangeover a string decodes UTF-8, yielding the byte index and rune (invalid bytes becomeU+FFFD). Use[]rune(s)orutf8.RuneCountInStringfor character work.