Go Mastery 4: Standard Library and Tooling
/ 2 min read
Table of Contents
Standard library you must know
| Area | Packages |
|---|---|
| I/O | io, bufio, os, io/fs, embed |
| Text | strings, bytes, strconv, unicode, regexp, text/template, html/template |
| Data formats | encoding/json, encoding/xml, encoding/csv, encoding/gob, encoding/base64 |
| Time | time (durations, formatting layout 2006-01-02, timezones, monotonic clock) |
| Networking | net, net/http, net/url, net/http/httptest |
| Logging | log, log/slog (structured logging, preferred) |
| Collections | slices, maps, container/heap, container/list |
| Math | math, math/rand/v2, math/big |
| Crypto | crypto/sha256, crypto/rand, crypto/tls, crypto/hmac |
| System | os/exec, os/signal, flag, path/filepath |
| Database | database/sql |
| Misc | sort, errors, fmt, testing, context, sync, runtime |
Deep dives
-
io.Reader/io.Writercomposition:io.Copy,io.TeeReader,io.MultiWriter,io.Pipe -
bufio.Scanner(and its token size limit) vsbufio.Reader -
encoding/json: tags,omitempty,json.RawMessage, customMarshalJSON, streaming withDecoder/Encoder -
net/http:Client(timeouts!),Server,Handler,ServeMux(method and wildcard routing in 1.22+), middleware -
time: never usetime.Sleepfor sync; useTimer/Tickerand stop them -
log/slog: handlers, levels, attributes, groups -
flagand building CLIs (also look atcobra) -
embedfor static files
Tooling
Core commands
go run / build / installgo fmt ./... # or gofmt -sgo vet ./...go test ./... -race -covergo mod tidygo doc <pkg>go generatego envgo list -m allgo workMust-use tools
- gopls: language server (VS Code Go extension / GoLand)
- golangci-lint: lint aggregator
- staticcheck
- govulncheck: security vulnerabilities
- delve (
dlv): debugger - goimports
- air (live reload), mockgen / mockery (mocks)
- sqlc, goose / migrate (DB)
Build and release
- Cross-compilation:
GOOS=linux GOARCH=amd64 go build - Build tags / constraints
-
-ldflags "-s -w -X main.version=..." - Static binaries with
CGO_ENABLED=0 - Multi-stage Docker builds (final image
scratchordistroless) - GoReleaser
Visual: a typical CI pipeline
flowchart LR A["git push"] --> B["gofmt / goimports"] B --> C["go vet"] C --> D["golangci-lint"] D --> E["go test -race -cover"] E --> F["govulncheck"] F --> G["go build (CGO_ENABLED=0)"] G --> H["Docker image"]
Practice
- Build a CLI tool (e.g.
grepclone) using only the stdlib - Write a JSON log analyzer using
bufio+encoding/json - Add
golangci-lint+govulncheckto a project’s CI
Previous: Go Mastery 3: Concurrency Done Right
Next: Go Mastery 5: Testing, Profiling and Internals