Skip to content
Tutorials Center
My learning

Topics

Go Reference · bytes, unicode and text

Go unicode package

Overview of Go's unicode package: test whether a rune is a letter, digit, space or punctuation, change case, and match script tables such as unicode.Han.

The unicode package answers questions about single runes: is it a letter? a digit? white space? upper case? It also converts case and exposes the Unicode category and script tables behind those answers, such as unicode.Letter, unicode.Han or unicode.Cyrillic.

It works on one rune at a time and knows nothing about encoding; for decoding and counting UTF-8 bytes use unicode/utf8. To process a string, loop over it with for _, r := range s and test each rune.

Go
for _, r := range "Go 1.27!" {
	fmt.Println(string(r), unicode.IsLetter(r))
}

Try it

Go Counting character kinds

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Go Case mapping and script tables

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

What the package contains

Group Members
Predicates IsLetter, IsDigit, IsSpace, IsNumber, IsUpper, IsLower, IsTitle, IsPunct, IsSymbol, IsControl, IsMark, IsPrint, IsGraphic
Case ToUpper, ToLower, ToTitle, To, SimpleFold
Tables Categories (Letter, Lu, Nd, ...), scripts (Latin, Greek, Han, ...), properties (White_Space, ...)
Matching Is(table, r), In(r, tables...), IsOneOf(tables, r)
Constants MaxRune, ReplacementChar, MaxASCII, MaxLatin1, Version

Usage notes

  • Rune-wise, not string-wise. Case mapping here is one rune to one rune, so ToUpper('ß') stays ß; Go does not expand it to SS. Whole-string helpers such as strings.ToUpper apply the same per-rune rules.
  • Predicates are broader than ASCII. IsLetter('ö') and IsLetter('世') are true; IsDigit accepts every decimal digit in Unicode (Arabic-Indic digits too).
  • IsDigit is narrow, IsNumber is wide. ½ and ² are numbers but not digits.
  • Not the same as regexp classes. \p{L} in regexp uses these same tables, so unicode.Letter and \p{L} agree.
  • Fast paths for Latin-1. The predicates check small lookup tables first, so ASCII text is cheap to classify.
  • Custom classes: build a *unicode.RangeTable or use strings.FieldsFunc / strings.TrimFunc with a predicate to apply them to strings.
  • Normalization and collation (accents, sorting by locale) live in golang.org/x/text, not the standard library.