Junior — Middle
Kuidas on stringid rakendatud ja kuidas nad on seotud rune'idega Go keeles?
sobes.tech AI
Vastus AI-lt
In the Go language, strings are represented by the string type, which is an immutable sequence of bytes, usually encoded in UTF-8. This means that a string is a sequence of bytes, not characters.
Runes (rune) are a type that represents a Unicode code point (int32). To work with Unicode characters in a string, the string is converted into a sequence of runes, which allows correct handling of multibyte characters.
Example:
s := "Привет"
for i, r := range s {
fmt.Printf("%d: %c\n", i, r) // i — byte index, r — rune (character)
}
runes := []rune(s) // convert string to a sequence of runes
fmt.Println(runes[0]) // first character as a rune
Thus, in Go, strings are sequences of bytes, and runes are individual Unicode characters, which allows correct working with multilingual texts.