RUNE is an alias type for INT32, where a value represents a Unicode character. Byte is an alias type for uint8, and a value is an ASCII code value. The rune type values are all represented by a UTF-8 encoded value at the bottom.
Unicode characters are common in Chinese, English, Japanese, or complex characters. For example, ‘G’, ‘o’, ‘love’, ‘good’, and ‘he’ are Unicode characters. 2, characters stored in the computer need to use binary numbers to represent. So they defined a table that represented the characters that we were using as a binary number. This is where the ASCII code table comes from.
The UTF-8 encoding scheme encodes a Unicode character into a sequence of bytes of length between 1 and 4. So, a rune value represents a byte array of 1 to 4 lengths.
Case study:
Printf("The string: %q\n", STR) fmt.printf (" => runes(char): %q\n", []rune(str)) fmt.Printf(" => runes(hex): %x\n", []rune(str)) fmt.Printf(" => bytes(hex): [x] % \ n ", byte [] (STR))} The string: "Go lovers" = > with runes (char) : [' G ' 'o' 'love' 'good' 'The'] = > with runes (hex) : [47 6f 7231 597d 8005] => bytes(hex): [47 6f e7 88 b1 e5 a5 bd e8 80 85]
The string value “Go hobbyist “is converted to a []rune value, and each character (whether in English or Chinese) becomes a rune element value independently. Such as the second line of printed content. Also, because each RUNE type value is represented by a UTF-8 encoding value at the bottom, such as the third line that splits the UTF-8 encoding value of each character into the corresponding sequence of bytes, such as the fourth line, since the UTF-8 encoding value of a Chinese character needs to be represented by three bytes.
Summary: A string value can be split into either a sequence of characters or a sequence of bytes. The former can be represented by a slice of element type rune, while the latter can be represented by a slice of element type byte.
Supplement:
fmt.Println("\u0070\x68\x65\x72") fmt.Println(`"\u0070\x68\x65\x72"`) Fmt.println (strconv.unquote (' "\u0070\x68\x65\x72" ')) output: pHER "u0070x68x65x72" pHER <nil>