Skip to content

Table of Contents

Month 2 — Runnable Examples

Single-concept, stdlib-only programs backing the Month 2 daily notes (standard library, tooling & testing). Each folder is its own package main.

How to run

go run ./examples/month-02/<topic>

Index (Week 1 — I/O, formatting & string packages)

Topic Concept Day
io-basics/ io.Reader/io.Writer, io.Copy, TeeReader, MultiWriter, LimitReader, the Read loop 029
bufio-scan/ bufio.Scanner (lines/words/custom split), token-too-long trap, bufio.Writer + Flush 030
fmt-verbs/ %v/%+v/%#v/%T, width/precision/flags, Stringer, arg indexing, %w wrap 032
strings-strconv/ strings helpers, strconv parse/format, strings.Builder, bytes.Buffer 033–034

Index (Week 2 — encoding, time, http, context & logging)

Topic Concept Day
json-tags/ encoding/json marshal/unmarshal, struct tags (omitempty/-), custom Marshaler/Unmarshaler, Decoder streaming 036–037
time-timers/ time.Duration, the reference layout 2006-01-02 15:04:05, time.After, Timer, Ticker, time.Since 038
http-context/ net/http client (NewRequestWithContext, Client.Do, Body.Close) + context timeouts/cancel via an in-process httptest.Server 039–040
slog-structured/ log/slog: JSON/Text handlers, levels, typed attrs, With child loggers, Group, SetDefault 041

Index (Week 3 — testing, benchmarks & fuzzing)

Topic Concept Day
httptest-demo/ httptest.NewRecorder (in-process) vs httptest.NewServer (real port) 045
bench-demo/ hand-rolled b.N loop: ns/op, sink the result, the += O(n²) trap 047
fuzz-roundtrip/ property/invariant testing over random inputs; a UTF-8 byte-reverse bug 048

Index (Week 4 — tooling, profiling, build & a project)

Topic Concept Day
build-tags/ //go:build constraints (OS + custom -tags), filename suffixes, cross-compilation with GOOS/GOARCH 052
pprof/ runtime/pprof CPU+heap profiles to a file and live net/http/pprof endpoints; analyze with go tool pprof 053
urlshortener/ stdlib net/http URL shortener: base62 codes, sync.RWMutex store, method checks, 302 redirects 055

build-tags/ is several files in one package main: each per-OS file carries a mutually exclusive //go:build line, plus debug_on.go/debug_off.go gated on -tags debug. Run with go run -tags debug ./examples/month-02/build-tags.

go vet, golangci-lint, Makefile, and GitHub Actions (Days 050, 051, 054) are tooling, not runnable programs, so they stay as snippets in the day notes. golangci-lint and staticcheck are third-party binaries (run as tools, never imported), so no example imports them.

The core testing artifacts (table-driven t.Run tests, t.Parallel/t.Helper, testing.B benchmarks, testing.F fuzz tests, Example functions) live in *_test.go files, so they ship as the Week 3 exercises rather than package main examples. testify is third-party, so it stays a snippet in the Day 046 note.

Day 031 (os/files/exit codes) has side effects (it writes files and calls os.Exit), so it stays as a snippet inside the day note rather than a runnable example here.

Conventions

  • One concept per folder; heavily commented as teaching artifacts.
  • Standard library only — these compile under the repo root module.

Examples