Junior — Middle
Millest koosneb string Go keeles?
sobes.tech AI
Vastus AI-lt
In the Go language, a string represents an immutable sequence of bytes, commonly used to store text in UTF-8 encoding. The main components of a string in Go:
- Bytes — the sequence of bytes that contains the data.
- Length — the number of bytes in the string.
Internally, the string is implemented as a structure with a pointer to a byte array and a length. The string does not contain an explicit separator or terminator.
It is important to understand that a string is not an array of characters (runes), but a sequence of bytes. To work with Unicode characters, the rune type and functions from the unicode/utf8 package are used.
Example:
s := "Привет"
fmt.Println(len(s)) // Displays the number of bytes, not characters
runes := []rune(s)
fmt.Println(len(runes)) // Number of characters (runes)