Go

Notes on the Go programming language — interfaces, concurrency, testing, and patterns that have held up in production.

Capturing console output in Go tests

Test functions that write to stdout/stderr in Go by capturing output with os.Pipe. Learn patterns to avoid deadlocks in concurrent tests.

Deferred teardown closure in Go testing

Return teardown closures from test helpers to manage cleanup elegantly. Learn patterns for temp files, mock servers, and t.Cleanup() usage.

Three flavors of sorting Go slices

Compare three Go slice sorting methods: sort.Interface, sort.Slice with closures, and modern generic slices.Sort with type safety.

Nil comparisons and Go interface

Debug tricky nil comparisons in Go interfaces. Understand dynamic types, type assertions, and use reflect for generic nil checking.

Stacked middleware vs embedded delegation in Go

Compare middleware stacking with embedded delegation in Go HTTP servers. Learn when to override ServeHTTP for simpler request handling.

Why does Go's io.Reader have such a weird signature?

Understand why io.Reader takes a byte slice parameter instead of returning one. Learn about heap allocations and buffer reuse in Go streams.

Go slice gotchas

Avoid common Go slice mistakes: shared backing arrays, nil vs empty slices, append behavior, and slice copying pitfalls explained.

Function types and single-method interfaces in Go

Implement single-method interfaces with function types instead of structs. Master http.HandlerFunc patterns for middlewares, mocks, and adapters.

Topological sort

Implement topological sorting in Go to order tasks by dependencies. Process directed acyclic graphs for build systems and scheduling.

Writing a circuit breaker in Go

Build a production-ready circuit breaker in Go from scratch with closed, open, and half-open states to prevent cascading failures.

Dysfunctional options pattern in Go

Discover a simpler alternative to functional options: method chaining with builder-style configuration that's 76x faster and easier to understand.

Strategy pattern in Go

Replace inheritance with the Strategy pattern in Go using interfaces. Achieve composable, testable code without class hierarchies.

Anemic stack traces in Go

Learn how to build custom error types in Go to create stack traces without runtime overhead, inspired by Rob Pike's Upspin error handling.

Retry function in Go

Build retry logic in Go without reflection using generics. Implement exponential backoff and configurable retry strategies with type safety.

Type assertion vs type switches in Go

Master Go type assertions with i.(T) syntax and type switches. Extract concrete types from interfaces safely with ok idiom examples.