Why does fmt.Printf("%x", 'ᚵ') ~> 16b5, while fmt.Printf("%x", "ᚵ") ~> e19ab5?

Viewed 72
1 Answers

Because each does a different thing. Both format the argument as a hexadecimal number, but each views the argument differently.

fmt.Printf("%x", 'ᚵ') prints a single unicode character (a rune, if you will), as a 32 bit integer (int32).

fmt.Printf("%x", "ᚵ") prints a string (individual bytes of a string) as 8 bit integers (uint8). The rune is encoded on three bytes when utf-8 encoding is used. That is a reason why there is six hexadecimal digits (two for each byte).

To study printing of a string in detail, start at function fmtString in file fmt/print.go.

func (p *pp) fmtString(v string, verb rune) {
Related