• Strings are presented as binary numbers in computers, one way of doing this was ASCII encoding where each letter got its own ASCII value. In Go, strings are encoded in UTF-8 even for cases like special characters, Chinese letters or emojis. Non-ASCII characters use 2 or more bytes.
    • Example for word résumé:

      func main() {
      	var myString = "résumé"
      	for i, r := range myString {
      		fmt.Printf("Index: %d, Rune: %c\n", i, r)
      	}
      }
      
      /* output:
      Index: 0, Rune: r
      Index: 1, Rune: é
      Index: 3, Rune: s
      Index: 4, Rune: u
      Index: 5, Rune: m
      Index: 6, Rune: é */
      
      // Iterating over the string (returns runes and their starting indices)
      	for i, v := range myString {
      		fmt.Println(i, v)
      	}
      /* output:
      0 114
      1 233
      3 115
      4 117
      5 109
      6 233 */
      
      • You'll notice that special characters have taken two extra indexes (there is even index 7 after index 6), because the é character is two bytes.

        		r         é                    s         u        m        é
        [01110010, 11000011, 10101001, 01110011, 01110101, 01101101, 11000011, 10101001]
        
      • The reason why index 1 is 233 is because the range keyword knows that its a 2 bytes character so it decodes correctly to 233 (the bytes in the screenshot above).

      • An easier way to deal with iterating and indexing strings is to cast them to an array of runes for such case (rune is a single Unicode code point, basically just an alias for int32).

        func main() {
        	var myString = []rune("résumé")
        	for i, v := range myString {
        		fmt.Println(i, v)
        	}
        }
        
        /* output:
        0 114
        1 233
        2 115
        3 117
        4 109
        5 233 */
        
      • Strings are bytes, not characters. Runes represent actual characters

      • Check this part of the video for more.

    • When you're dealing with strings in Go you're dealing with a value whose underlying representation is an array of bytes.

    • You should use runes when you need to work with individual characters in a string and handle text that includes characters from different languages, including non-ASCII characters.