Adding one character at a time

yourbasic.org/golang

Why doesn’t these print statements give the same result?

fmt.Println("H" + "i")
fmt.Println('H' + 'i')
Hi
177

Answer

The rune literals 'H' and 'i' are integer values identifying Unicode code points: 'H' is 72 and 'i' is 105.

You can turn a code point into a string with a conversion.

fmt.Println(string(72) + string('i')) // "Hi"

You can also use the fmt.Sprintf function.

s := fmt.Sprintf("%c%c, world!", 72, 'i')
fmt.Println(s)// "Hi, world!"

The fmt cheat sheet lists the most common formatting verbs and flags.

Share this page: