Strings and Runes
fmt unicode/utf8A Go string is a read-only slice of bytes. The language and the standard library treat strings as containers of text encoded in UTF-8. In other languages strings are made of characters; in Go, a character is called a rune, an integer that represents a Unicode code point.
package main
import (
"fmt"
"unicode/utf8"
)
func main() {s is a string holding the word "hello" in Thai. String literals are UTF-8 encoded text.
const s = "สวัสดี"Since strings are bytes, len gives the number of raw bytes.
fmt.Println("Len:", len(s))Indexing a string gives the byte at each index.
for i := 0; i < len(s); i++ {
fmt.Printf("%x ", s[i])
}
fmt.Println()To count runes, use the utf8 package. Some Thai characters are made of several code points, so the count may surprise you.
fmt.Println("Rune count:", utf8.RuneCountInString(s))range decodes each rune and its byte offset.
for idx, runeValue := range s {
fmt.Printf("%#U starts at %d\n", runeValue, idx)
}The same iteration with utf8.DecodeRuneInString.
fmt.Println("\nUsing DecodeRuneInString")
for i, w := 0, 0; i < len(s); i += w {
runeValue, width := utf8.DecodeRuneInString(s[i:])
fmt.Printf("%#U starts at %d\n", runeValue, i)
w = widthA rune can be passed to a function and compared to a rune literal.
examineRune(runeValue)
}Strings cannot be changed in place. Convert to []rune or []byte, change that, and convert back.
r := []rune("hello")
r[0] = 'j'
fmt.Println(string(r))Raw string literals use backquotes: no escapes, and they can span lines.
fmt.Println(`C:\no\escapes`)
}
func examineRune(r rune) {Values in single quotes are rune literals.
if r == 't' {
fmt.Println("found tee")
} else if r == 'ส' {
fmt.Println("found so sua")
}
}$ go run ./18_strings_and_runes Len: 18 e0 b8 aa e0 b8 a7 e0 b8 b1 e0 b8 aa e0 b8 94 e0 b8 b5 Rune count: 6 U+0E2A 'ส' starts at 0 U+0E27 'ว' starts at 3 U+0E31 'ั' starts at 6 U+0E2A 'ส' starts at 9 U+0E14 'ด' starts at 12 U+0E35 'ี' starts at 15 Using DecodeRuneInString U+0E2A 'ส' starts at 0 found so sua U+0E27 'ว' starts at 3 U+0E31 'ั' starts at 6 U+0E2A 'ส' starts at 9 found so sua U+0E14 'ด' starts at 12 U+0E35 'ี' starts at 15 jello C:\no\escapes