Junior — Middle
Selgitage teksti tüübi kontseptsiooni Go keeles ja selle peamised omadused.
sobes.tech AI
Vastus AI-lt
In the Go language, the string type represents an immutable sequence of bytes, commonly used to store text encoded in UTF-8.
Main features of strings in Go:
- Immutability: Once created, a string cannot be changed. Any operation that modifies the string creates a new one.
- UTF-8 encoding: Strings are stored in UTF-8, allowing work with multilingual text.
- Access by bytes: Indexing a string returns a byte, not a character (rune). To work with characters, the
runetype is used. - Slice support: Substrings can be obtained using slices, e.g.,
s[0:5].
Example of working with strings and runes:
s := "Привет"
fmt.Println(len(s)) // number of bytes
for i, r := range s {
fmt.Printf("%d: %c\n", i, r) // prints byte index and character
}
Thus, strings in Go are a convenient and efficient way to work with text, but it is important to understand the difference between bytes and characters.