What exactly is a rune datatype in Go

Kvs Vishnu Kumar
3 min readNov 8, 2021

Describing a rune datatype in golang

To understand rune datatype, we must understand some basic concepts. First is, what exactly is a string.

String Datatype

The normal definition of a string is that it is a combination of characters. This is our understanding. But remember the actual definition. A string is a collection of bytes. This collection of bytes can be accessed as a whole or as an array.

What exactly is a byte. A byte is an integer value between 0 to 255. A byte is used to store a character. Therefore a string becomes a collection of characters

ASCII vs UNICODE

Now, we need to understand characters. There are different types of characters such as digits, alphabets, symbols, emoji’s etc. To standardize all characters, we have some encoding standards. The two most popular encoding standards are ASCII and UNICODE.

ASCII

source: google images

From the above table, you can see that there are total 127 characters. So one byte is sufficient to represent an ASCII character.

UNICODE

If you work in web development, you might have come across UTF-8. It is a Unicode encoding standard that is widely used on the internet. Almost 97% web pages use UTF-8.

UTF-8 is capable of encoding all 1,112,064 valid character code points in Unicode. A Unicode character can take utmost 4 bytes.

As a matter of fact, ASCII is a subset of Unicode.

Click Here to check the Unicode chart.

Rune

Now that you understand what are characters and encoding schemes, here comes the role of rune datatype.

Definition: A rune is an int32 value that is used for representing a single Unicode code point.

Remember rune is an alias for int32 value which represents a single Unicode point. From the above program to print the rune equivalent of 128513, we used Printf() function with %c control string.

Important: Always remember a string is a sequence of bytes,not of a rune. But it is possible that a string may contain Unicode text encoded in UTF-8.

To get better understanding, go through the programs below:

Program 1

Program 2

Here 7272 is not a sequence of bytes, its a rune.

Program 3

Program 4

Finally, I hope you got a better understanding of rune datatype. 😀 😄

--

--