A string is an immutable sequence of bytes. Strings may contain arbitrary data, including bytes with value 0, but usually they contain human-readable text. Text strings are conventionally interpreted as UTF-8-encoded sequences of Unicode code points (runes), which we’ll explore in detail very soon.
len function returns the number of bytes (not runes) in a string,
and the index operation
s[i] retrieves the i-th byte of
s, where 0 ≤
s := "hello, world" fmt.Println(len(s)) // "12" fmt.Println(s, s) // "104 119" ('h' and 'w')
Attempting to access a byte outside this range results in a panic:
c := s[len(s)] // panic: index out of ...