In order to start our discussion about the rune
and string
types, some background context is in order. Go can treat character and string literal constants in its source code as Unicode. It is a global standard whose goal is to catalog symbols for known writing systems by assigning a numerical value (known as code point) to each character.
By default, Go inherently supports UTF-8 which is an efficient way of encoding and storing Unicode numerical values. That is all the background needed to continue with this subject. No further detail will be discussed as it is beyond the scope of this book.
So, what exactly does the rune
type have to do with Unicode? The rune is an alias for the int32 type. It is specifically intended to store Unicode integer values encoded as UTF-8. Let us take a look at some rune literals in the following program:
Each variable in the previous program stores a Unicode character as a rune
value. In Go, the rune
may be specified as a string literal constant surrounded by single quotes. The literal may be one of the following:
char1
, char2
, and char3
)u
followed by Unicode values directly (u0369
)x
followed by two hex digits