Junior — Middle
What happens when iterating over string elements in programming?
sobes.tech AI
Answer from AI
In Go, when iterating over a string using a for range loop, the iteration is over Unicode code points (runes), not bytes. This means that each element of the loop is a Unicode character, which can occupy multiple bytes in UTF-8.
For example:
s := "Привет"
for i, r := range s {
fmt.Printf("Index: %d, Rune: %c\n", i, r)
}
Here, i is the index of the first byte of the rune in the string, and r is the rune itself (the character).
Thus, iterating over a string in Go allows for correct handling of multilingual characters and does not break multi-byte UTF-8 characters.