Search⌘ K

Puzzle 20 Explanation: Go Rune

Explore the concept of runes in Go, understanding how strings are UTF-8 encoded and accessed at the byte level. Learn to iterate over strings to retrieve Unicode characters as runes, and see how different formatting verbs display runes. This lesson helps you grasp the numeric and character representations of runes and how to write rune literals effectively in Go.

We'll cover the following...

Try it yourself

Try executing the code below to see the result.

Go (1.16.5)
package main
import (
"fmt"
)
func main() {
msg := "π = 3.14159265358..."
fmt.Printf("%T ", msg[0])
for _, c := range msg {
fmt.Printf("%T\n", c)
break
}
}

Explanation

Go strings are UTF-8 encoded. Go will access the underlying []byte when we access a string with ...