HowtoGo
Strings & Text

Runes

Terms Rune

A rune is Go's name for a Unicode code point, an alias for int32. The unicode and unicode/utf8 packages classify, convert, and decode them.

Examples

A rune is an alias for int32, one Unicode code point. string(r) re-encodes it back to UTF-8; converting a string to []rune decodes it the other way.

r := 'A'
fmt.Printf("%c %d %U\n", r, r, r)

s := string(r)
fmt.Println(s)

runes := []rune("héllo")
fmt.Println(len(runes), len("héllo"))
Output
A 65 U+0041
A
5 6
FunctionDescription
string(r)
string('A')
Converts a single rune to its one-character string
[]rune(s)
[]rune("héllo")
Decodes a string into its sequence of runes
unicode.IsLetter(r)
unicode.IsLetter('G')
Reports whether r is a letter
unicode.IsDigit(r)
unicode.IsDigit('3')
Reports whether r is a decimal digit
unicode.IsSpace(r)
unicode.IsSpace(' ')
Reports whether r is a space, tab, newline, or similar
unicode.IsPunct(r)
unicode.IsPunct('!')
Reports whether r is a punctuation character
unicode.IsUpper(r) / IsLower(r)
unicode.IsUpper('A')
Reports whether r is upper- or lowercase
unicode.ToUpper(r) / ToLower(r)
unicode.ToUpper('a')
Returns the upper- or lowercase form of r
utf8.RuneLen(r)
utf8.RuneLen('é')
Number of bytes r takes when UTF-8 encoded
utf8.ValidRune(r)
utf8.ValidRune(r)
Reports whether r is a valid Unicode code point
utf8.RuneCountInString(s)
utf8.RuneCountInString("héllo")
Number of runes in s, as opposed to bytes
utf8.DecodeRuneInString(s)
r, size := utf8.DecodeRuneInString(s)
Decodes the first rune in s, returning it and its byte width
Related: Strings unicode unicode/utf8