October 2015
Beginner to intermediate
400 pages
14h 44m
English
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.
The built-in len function returns the number of bytes (not runes) in a string,
and the index operation s[i] retrieves the i-th byte of
string s, where 0 ≤ i < len(s).
s := "hello, world"
fmt.Println(len(s)) // "12"
fmt.Println(s[0], s[7]) // "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 ...