Standard Library Tour Overview
Standard Library Tour Overview
This part is a stdlib-first map of the packages you will use on almost every real Go program. Frameworks and third-party libraries come later; fluency here is what makes those tools feel thin and optional.
Earlier parts already teach language mechanics. Here the unit of study is the package contract: what it guarantees, what it leaves to you, and which defaults are production-safe.
Why This Part Exists
Most “I know Go” gaps are not syntax. They are:
- Using
fmt.Printlnwherelog/slogbelongs - Loading whole files when
iostreams would bound memory http.Getwith no timeout- Stringly-typed paths instead of
filepath/io/fs - Hand-rolled JSON error handling that loses type safety
- Concurrency without
contextdeadlines orsyncownership rules
The standard library already solves these — if you know where to look.
Learning Path
| Chapter | Packages | Outcome |
|---|---|---|
| 981 fmt / log / slog | fmt, log, log/slog |
Readable output and structured events |
| 982 Text & numbers | bytes, strings, strconv, unicode, utf8 |
Correct UTF-8 and conversions |
| 983 I/O composition | io, bufio |
Bounded streams and efficient copies |
| 984 Files & embed | os, path/filepath, io/fs, embed |
Safe paths and single-binary assets |
| 985 Encoding | encoding/json, encoding/json/v2, csv, xml, base64, hex |
Data at rest and on the wire |
| 986 net/http | net/http, net/url |
Production-shaped clients and servers |
| 987 CLI & process | flag, os/exec, os/signal |
Tools that parse, run, and shut down cleanly |
| 988 time & context | time, context |
Budgets, clocks, and cancellation |
| 989 sync & atomic | sync, sync/atomic |
Shared state without races |
| 990 Collections | slices, maps, cmp, iter |
Modern helpers (Go 1.21+) |
| 991 testing | testing, testing/fstest, net/http/httptest |
Tests, examples, fuzz, benches |
| 992 Crypto & text scan | crypto/*, uuid, hash, math/rand/v2, regexp |
Integrity, IDs, secrets hygiene, matching |
| 993 database/sql | database/sql |
Pools, queries, transactions |
| 994 net dial/listen | net |
TCP/UDP/Unix under HTTP |
| 995 templates | text/template, html/template |
Safe rendering and codegen |
| 996 compress/archive | compress/*, archive/zip, tar |
gzip, zip, tar streams |
| 997 binary / gob / pem | encoding/binary, gob, pem |
Frames, Go blobs, cert armor |
| 998 math / bits / big | math, math/bits, math/big |
Floats, bit ops, big integers |
| 999 mime / ops | mime, multipart, expvar, runtime/debug |
Uploads, debug vars, build info |
| 999a containers | container/heap, list, ring |
Priority queues, lists |
| 999b path/filepath | path, path/filepath |
OS paths vs slash paths, jails |
| 999c bufio advanced | bufio Scanner/Reader/Writer |
Long lines, peek, flush |
Mental Model
input -> decode / parse -> domain logic -> encode / write -> output
| | |
encoding/* context io / os / net
flag / json time slog for events
Almost every program is that pipeline with different endpoints (CLI, HTTP, file, pipe).
Package Selection Cheatsheet
| Need | Prefer | Avoid by default |
|---|---|---|
| Logs for ops | log/slog |
fmt.Println in libraries |
| HTTP service | net/http + std mux (1.22+) |
Framework until you need it |
| SQL access | database/sql + driver |
String-built queries |
| Raw sockets | net.Dialer / Listen + deadlines |
Untimed Accept/Read loops |
| HTML pages | html/template |
text/template for untrusted HTML |
| gzip / zip / tar | compress/gzip, archive/* |
Shelling out for common formats |
| Binary frames | encoding/binary + max length |
Trust remote length blindly |
| Go-to-Go cache | gob (versioned) |
gob as a public multi-lang API |
| Config flags | flag (or small wrapper) |
Global env soup without docs |
| File walk | fs.WalkDir / filepath.WalkDir |
Manual recursion without errors |
| JSON API | encoding/json |
interface{} everywhere |
| Timeouts | context + http.Client.Timeout |
Fire-and-forget goroutines |
| Shared map | sync.Mutex or sync.Map (when justified) |
Unsynchronized maps |
| Sort / search | slices |
Hand-rolled O(n²) loops |
| Big integers | math/big |
float64 for money / huge ints |
| Uploads | mime/multipart + size limits |
Unbounded ParseMultipartForm |
| Secrets material | crypto/rand, careful logging |
math/rand for tokens |
Relationship to Other Parts
| Topic | Deeper part |
|---|---|
| Streaming I/O patterns | 12 Specialized — I/O |
| Concurrency design | 07 Concurrency |
| HTTP resilience | 13 Network systems |
| TCP services | 13 Network systems — TCP |
| SQL pools | 20 Deep dives — database/sql |
| Web templates / DB | 22 Web development |
| slog in production | 16 Observability |
| TLS / crypto hardening | 17 Security |
| Profiling | 18 Performance |
Use this part as the default toolkit. Jump to specialized parts when the problem is no longer “which package?” but “how does this fail under load?”
How to Study
- Read the index map; pick one vertical (CLI tool or tiny HTTP service).
- For each chapter, run the runnable example once, then change one assumption (timeout, limit, encoding).
- Rebuild a small project using only stdlib until you feel friction — that friction is when a library earns its place.
stdlib literacy loop
read package docs -> run example -> break limits -> fix with contracts -> ship tiny tool
Checklist: Stdlib Literacy
- Can structure logs with
slogand attach request-scoped fields - Can stream with
io.Copy/LimitReaderinstead of unboundedReadAll - Can open, walk, and embed files without path traversal bugs
- Can marshal/unmarshal JSON with sensible error surfaces
- Can serve and call HTTP with timeouts and context
- Can build a multi-flag CLI that exits non-zero on failure
- Can cancel work with
contextand wait withsync.WaitGroup - Can write table tests, an example, and a basic fuzz target
- Can generate tokens with
crypto/randand hash withcrypto/sha256 - Can open
database/sql, query with context, and mapErrNoRows - Can dial/listen on TCP with deadlines (not only HTTP)
- Can render HTML safely with
html/template(parse once) - Can gzip a stream and extract zip/tar without path traversal
- Can write a length-prefixed binary frame with a max size
- Can accept a size-limited multipart upload
- Can expose version/
expvaror readdebug.ReadBuildInfo
When most boxes are checked, third-party libraries become choices — not crutches.
Suggested extensions path
Core loop (981–992) text → io → files → json → http → cli → time → sync → tests → crypto
Data & wire (993–997) sql → net → templates → compress → binary/gob/pem
Math & ops (998–999) bits/big → mime/uploads → expvar/pprof/buildinfo
Extras (999a–c) container heap/list/ring → path/filepath → bufio advanced