Search⌘ K

Puzzle 15 Explanation: Length of Each Variable

Explore how Go's UTF-8 string encoding affects variable lengths and string comparison. Understand the differences between Normalization Forms NFC and NFD, and learn to use external libraries like golang.org/x/text/norm to normalize strings for accurate comparisons.

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() {
city1, city2 := "Krakow", "Kraków"
fmt.Println(city1 == city2)
}

Explanation

In the code above, do the two strings look similar? Yes. However, if we print the length of each variable, the length of city1 will be 6, and the length of city2 will be 7.

Go’s strings are UTF-8 encoded byte ...