Golang & Concurrency (50 Qs)•Part 1: Go Language Core, Memory & Runtime Internals (1 - 15)
Strings, Runes, and Bytes: UTF-8 Representation and Zero-Copy Conversions.
senior LevelSvelte 5
In Simple Words:
- A
stringis an immutable 16-byte slice header pointing to read-only UTF-8 bytes (pointer,len). byteis an alias foruint8.runeis an alias forint32representing a Unicode Code Point.- Iterating with
for i := 0; i < len(s); i++accesses raw bytes; iterating withfor _, r := range sdecodes Unicode runes. - In Go 1.20+, use
unsafe.Stringandunsafe.SliceDatafor zero-allocation conversions between[]byteandstring.