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.
for _, r := range "Go 1.27!" { fmt.Println(string(r), unicode.IsLetter(r)) }
Try it
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.
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 toSS. Whole-string helpers such asstrings.ToUpperapply the same per-rune rules. - Predicates are broader than ASCII.
IsLetter('ö')andIsLetter('世')are true;IsDigitaccepts every decimal digit in Unicode (Arabic-Indic digits too). IsDigitis narrow,IsNumberis wide.½and²are numbers but not digits.- Not the same as regexp classes.
\p{L}inregexpuses these same tables, sounicode.Letterand\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.RangeTableor usestrings.FieldsFunc/strings.TrimFuncwith a predicate to apply them to strings. - Normalization and collation (accents, sorting by locale) live in
golang.org/x/text, not the standard library.