Rune in Golang
Overview
In the past, ASCII (American Standard Code for Information Interchange) was the only character set available. There, 128 characters, including upper- and lowercase English letters, numerals, a range of punctuation, and device-control characters, were represented by 7 bits. The bulk of the population is unable to use their unique writing systems as a result of this character restriction. Unicode was created to address this issue. All characters used in the current global writing system are included in the superset of ASCII known as Unicode. It includes accents, diacritical markings, and control codes like tab and carriage return, and it gives each letter a standard number called a "Unicode Code Point" (or rune in golang).
Declaration
You can learn how to initialize a rune variable by using the model below:

Important Points about Rune in Golang
- Remember that a string is a series of bytes, not of a Rune. Unicode content encoded in UTF-8 may be found in a string. Encoding the string in UTF-8 is unnecessary because the Go source code already does so.
- The Unicode is encoded using UTF-8 in a range of 1 to 4 bytes, with 1 byte used for ASCII and the remaining 3 for Runes.
- A total of 256 elements comprise ASCII, of which 128 are characters, and the remaining 0-127 are known as code points. In this context, a code point is an element that designates a single value.
Rune Literal
It represents a Rune constant in which a Unicode code point is identified by an integer value. A rune literal is expressed in the Go language as one or more characters in single quotes, such as "g," "t," etc. Any character may be inserted between single quotes, with the exception of a newline and an unescaped single quote.
In this case, these single-quoted characters indicate the Unicode value of the specified character, whereas multi-character sequences that begin with a backslash encode values in a different format. Only the following single-character escapes, when used with a backslash, represent special values in Rune Literals; all other sequences that begin with a backslash are not allowed:
| Character | Unicode | Description |
|---|---|---|
| \a | U+0007 | Represents an Alert or Bell |
| \b | U+0008 | Indicates a backspace |
| \f | U+000C | Represents a form feed |
| \n | U+000A | Denotes a line feed or newline |
| \r | U+000D | Signifies a carriage return |
| \t | U+0009 | Stands for a horizontal tab |
| \v | U+000b | Represents a vertical tab |
| (\\) | U+005c | It is used for backslash |
| \’ | U+0027 | It is used for single-quote |
| \” | U+0022 | It indicates a double quote(legal only in string literals) |
Rune as a Data Type
Rune in golang is used to store codes that correspond to Unicode characters. In reality, Unicode is a collection of every character that has ever existed. Each of these characters in Unicode has been given a special number known as a Unicode code point. We keep this code point in a rune data type.
Encoding Method
Go uses the UTF-8 encoding technique to store the Unicode characters in memory. An output encoded using this method has a variable length between 1-4 bytes. Because each rune may only contain an integer value of up to 32 bits, the rune is sometimes known as an alias for int32.
Character vs. Rune
In reality, each rune in golang only refers to one Unicode code point, hence each rune corresponds to one character.
Additionally, because Go does not have a char data type, any variables initialized with a character would immediately be typecast into int32, which in this instance, depicts a rune.
Although it might appear that rune in golang is the char equivalent. In Go, strings are actually composed of byte sequences rather than rune sequences.
We can think of rune as being distinct from the typical char data type found in other programming languages in this sense. Even when numerous runes are to be printed in a string, they must first be typecast into rune arrays.
In Go, if we explicitly give a value to a variable using:=, the compiler will determine the variable's data type. In this manner, the data type of a variable does not always need to be hard coded.
Output:
Example
To verify the data type and Unicode code point for a rune variable that we declared, we can use the code listed below:
Output:
Here, we can see that the 32-bit number used to store the value of our emoji is 0x1F600, and the encoding for our emoji is U+1F600.
Conclusion
- We understood the fundamentals of rune in golang, as well as how they function and what their syntax is. We also focus on some of its examples for understanding the runs for the character’s number and the symbols.