Puzzle 15 Explanation: Length of Each Variable
Learn about a pitfall related to UTF-8 encoding.
We'll cover the following...
Try it yourself
Try executing the code below to see the result.
Press + to interact
package mainimport ("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 ...