What is the rune type in Golang?

Rune as a data type

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 whole worldthis includes all characters from all dialects of all languages, and even includes emojis. In Unicode, each of these characters is assigned a unique number called the Unicode code point. This code point is what we store in a rune data type.

Encoding method

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.

Declaration

The following model shows you how to initialize a rune variable:

Character vs. rune

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 main
import "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)
}

Example

We can use the following program to check the data type and the Unicode code point for a rune variable that we declared:

package main
import "fmt"
func main() {
// declaring and initializing a Unicode character
emoji := '😀'
// %T represents the type of the variable num
fmt.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 U+1F600the rune value, and the 32-bit number used to store this value is 0x1F600.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved