Golang: Rune

By Xah Lee. Date: . Last updated: .

What's Rune

“Rune” means Unicode codepoint. (think of it as a character.) It is a jargon golang invented.

When you see the word “rune”, you can think of it as any or all of the following:

Purpose of Rune

The purpose of rune type is to directly represent one character. It is essentially a character type, just with a fancy name.

What's Codepoint

Unicode gives each character a integer id. This integer id is called codepoint.

Note, some codepoints are non-printable characters. For example, space, newline, tab, right-to-left mark, combining character for accents, etc.

[see Unicode Basics: Character Set, Encoding, UTF-8, Codepoint]

Unicode Standard Notation for Codepoint

Unicode has a standard notation for codepoint, starting with U+ followed by its codepoint in hexadecimal notation. Example:

Rune Literal

A “rune literal” is a syntax to represent one Unicode character in golang. Like this: 'c', where the c is 1 single Unicode character. (but may be presented by escape sequence)

All of the following are rune values.

To see all backslash escapes, see Golang: String Backslash Escape

Print a rune in decimal, hex, and standard unicode notations:

package main

import "fmt"

func main() {

	// print a rune in decimal, hex, and standard unicode notations

	var capA = 'A'

	fmt.Printf("%d %x %U\n", capA, capA, capA) // 65 41 U+0041
}

Print Rune

Since a rune value is a integer, we can use Printf formats that work with integer to print rune:

%c
print character as is (the byte sequence of the character in utf8 encoding.).
%q
print in golang rune syntax. Example: 'a'.
%U
print in Unicode notation. example: U+03B1.
%b
print in base 2
%o
print in base 8
%d
print in base 10
%x
print in base 16, with lower-case letters for a-f
package main

import "fmt"

// print rune in different formats

func main() {
	var x = '😂'

	fmt.Printf("%c\n", x) // 😂
	fmt.Printf("%q\n", x) // '😂'
	fmt.Printf("%U\n", x) // U+1F602

	fmt.Printf("%b\n", x) // 11111011000000010
	fmt.Printf("%o\n", x) // 373002
	fmt.Printf("%d\n", x) // 128514
	fmt.Printf("%x\n", x) // 1f602
}

Rune Sequence

Value of rune type is just a single char.

For a sequence of chars, you use slice type instead. (Slice is a variable length array.) A slice of rune is common, and can be converted to string.

See:

Reference

The Go Programming Language Specification - The Go Programming Language#String_literals

Golang

Examples

Reference