UTF-8 is a variable-width Unicode encoding that represents characters as sequences of one to four bytes. Go uses Unicode natively, while its rune type represents a Unicode code point. section 5.1.2
When handling Go strings, choose the representation that matches the operation:
- Use the string or []byte representation when measuring or accessing encoded bytes. section 5.1.2
- Use []rune when counting characters or iterating over Unicode code points. section 5.1.2
Bytes versus runes
fmt.Println(len("Hello, 世界")) prints 13 because the non-Latin characters occupy three bytes each. fmt.Println(len([]rune("Hello, 世界"))) prints 9, counting characters instead.










