Go's standard library processes text with the strings, bytes, unicode, regexp, bufio, text/template, and strconv packages, and it handles Unicode natively because strings are UTF-8. For analysis tasks such as word counts, tokenising, and log parsing, you combine bufio.Scanner for streaming input with strings functions, maps, and regexp.
The main rule is to pick the lightest tool that works: plain strings functions before regular expressions, and a streaming scanner before reading whole files into memory.
stringsregexpbufioA Go string is an immutable sequence of bytes, usually UTF-8. Indexing returns a byte, and range over a string yields runes (Unicode code points). This difference matters for any non-ASCII text.
Use strings.Builder when you assemble text in a loop, because repeated += copies the string every time. Use []byte and the bytes package when you modify data in place.
stringsThe strings package covers most everyday needs. Frequently used functions are Contains, HasPrefix, Fields, Split, Join, TrimSpace, ReplaceAll, EqualFold, and Cut.
strings.Cut is the cleanest way to split on the first separator. strings.SplitSeq and FieldsSeq return iterators from Go 1.24, which avoids allocating a slice. For case-insensitive comparison use strings.EqualFold, not ToLower on both sides.
regexpThe regexp package uses the RE2 syntax. It guarantees linear-time matching, so it is safe against catastrophic backtracking, but it has no lookahead or backreferences. Compile patterns once, at package level.
Regular expressions are slower than plain string functions, so do not use them for fixed substrings.
bufioRead big inputs line by line or word by word with bufio.Scanner so memory use stays constant. The default token limit is 64 KiB per line, and you raise it with scanner.Buffer for longer lines.
This is a complete word-frequency analysis: tokenise, normalise, count in a map, and sort by count. Always check sc.Err() after the loop.
text/template renders text from data, and html/template does the same with automatic escaping for HTML. Use html/template for anything that reaches a browser.
For converting between text and numbers, use strconv (Atoi, ParseFloat, Quote), and check the returned errors.
| Goal | Technique |
|---|---|
| Avoid copies | strings.Builder, bytes.Buffer, slicing instead of copying |
| Constant memory | bufio.Scanner or bufio.Reader streaming |
| Parallel analysis | Split input into chunks, count per goroutine, merge the maps |
| Correct Unicode handling | Iterate runes, use unicode predicates |
| Linguistic normalisation | golang.org/x/text (collation, normalisation, encodings) |
| Safe patterns | Compile regexp once; prefer strings for literals |
The standard library does not include natural language processing such as stemming, tagging, or sentiment analysis. Use external packages or call a service for those.
Use utf8.RuneCountInString(s) or len([]rune(s)). len(s) returns bytes, not characters.
regexp in Go slow?It is slower than hand-written string code, but it runs in linear time. Precompile patterns and avoid regular expressions for simple prefix, suffix, or substring checks.
Use bufio.Scanner with scanner.Buffer to raise the line limit if needed, or bufio.Reader.ReadString for unlimited line lengths.
The standard library assumes UTF-8. Use golang.org/x/text/encoding to decode other character sets.
Go treats text as UTF-8 bytes, with runes for characters. Reach for strings first, regexp for real patterns, bufio.Scanner for large input, and text/template or html/template for output. Count and sort with maps, parallelise by chunk when needed, and add golang.org/x/text for encodings and normalisation.