rune
in Go is a data type that stores codes that represent Unicode characters. Unicode is actually the collection of all possible characters present in the rune
data type.
To store the Unicode characters in memory, Go uses the UTF-8 encoding method. This method results in an encoded output of variable length from 1-4 Bytes. For this reason, rune
is also known as an alias for int32
, as each rune
can store an integer value of at most 32-bits.
The following model shows you how to initialize a rune
variable:
Each rune actually refers to only one Unicode code point, meaning one rune represents a single character.
Furthermore, Go does not have a char
data type, so all variables initialized with a character would automatically be typecasted
into int32
, which, in this case, represents a rune.
Here, it might seem like rune
is the char
alternative for Go, but that is not actually true. In Go, strings are actually made up of sequences of bytes, not a sequence of rune
.
In this regard, we can consider rune
to be different from the usual char
data type we see in other programming languages. Even when we need to print multiple rune
in a string, we must first typecast
them into rune arrays
.
In Go, we can have the compiler decide the data type of a variable if we directly assign it a value using
:=
. This way, we do not always have to hard code the data type of a variable.
We can see this here:
package mainimport "fmt"func main() {var str string = "ABCD"r_array := []rune(str)fmt.Printf("Array of rune values for A, B, C and D: %U\n", r_array)}
We can use the following program to check the data type and the Unicode code point for a rune
variable that we declared:
package mainimport "fmt"func main() {// declaring and initializing a Unicode characteremoji := '😀'// %T represents the type of the variable numfmt.Printf("Data type of %c is %T and the rune value is %U \n", emoji, emoji, emoji);}
Here, we can see that our emoji is encoded as rune
value