Go's standard library handles text and data processing with strings, strconv, regexp, bufio, unicode/utf8, text/template and the encoding/* packages. Combine strings.Builder for efficient string assembly, bufio.Scanner for streaming input and regexp for pattern matching, and you cover most real work without dependencies.
This guide covers each package with current idioms and the techniques that keep text code fast and correct, including how Go treats UTF-8.
stringsstrconvregexpstringsThe strings package holds the everyday operations. Prefer strings.Cut over index arithmetic when you split once, and strings.Fields when you split on any whitespace.
Since Go 1.24, strings.SplitSeq, strings.FieldsSeq and strings.Lines return iterators, so you can loop over pieces with for part := range strings.SplitSeq(s, ",") without allocating a slice. Strings are immutable, so every change creates a new string.
Concatenating in a loop with += copies the string each time. Use strings.Builder instead.
Call b.Grow(n) when you know the final size. Use strings.Join when you already have a slice of parts.
strconvstrconv converts between strings and numbers, booleans and quoted strings, and returns an error you must check.
Use strconv.ParseInt(s, base, bits) when you need a specific base or integer size. Do not use fmt.Sscanf for simple numeric parsing, because strconv is faster and gives clearer errors.
regexpGo's regexp package uses RE2 syntax, which guarantees linear-time matching and has no backreferences. Compile once, at package level, and reuse the result.
Use named groups with (?P<year>\d{4}) and SubexpNames() for readable extraction. For simple cases, strings functions are faster and clearer than a regular expression.
For large input, process line by line instead of loading everything with os.ReadFile.
bufio.Scanner limits a line to 64 KiB by default, so raise the limit with Buffer as shown if lines can be longer. For output, wrap the destination in bufio.NewWriter and call Flush.
A Go string is a sequence of bytes, usually UTF-8. len("héllo") counts bytes, and for i, r := range s yields runes. Use utf8.RuneCountInString(s) for the character count and unicode.IsLetter or unicode.ToUpper for rune-level logic.
For generating text, text/template fills placeholders from data (use html/template for HTML so output is escaped). For structured data, encoding/json, encoding/csv and encoding/xml decode into structs:
Use strings.Builder, or strings.Join for a slice. Both avoid the repeated copying of += in a loop.
len return a larger number than the characters I see?len counts bytes. Non-ASCII characters take two to four bytes in UTF-8, so count runes with utf8.RuneCountInString.
No. The RE2 engine omits lookaround and backreferences in exchange for guaranteed linear running time.
Stream it with bufio.Scanner or bufio.Reader, and write results through a bufio.Writer.
Use strings and strconv for most text work, strings.Builder for assembly, regexp when patterns are genuinely complex, and bufio to stream large input. Remember that strings are UTF-8 bytes, and use encoding/json, encoding/csv and templates for structured data.