Code-Memo

Advanced Go patterns

Functional options

See Topic 2 → args / decorators — configure APIs with ...Option for backward-compatible growth.

Interface segregation

Define small interfaces at the point of use (io.Reader, interface{ Write([]byte) (int, error) }).

Embedding for API extension

Embed http.Server or config structs sparingly; prefer explicit composition for clarity.

Table-driven tests

One test function, many cases in a slice of structs — standard library style.

Errors as values

Return error; wrap with %w; check with errors.Is / errors.As.

Dependency injection without frameworks

Pass interfaces into constructors; use fx/wire only when complexity warrants.

Build tags
//go:build linux

package main

Select files per OS or feature flags.

Avoid init side effects

init runs on import; keep it minimal — global registration only when necessary.

Versioned APIs

For public modules, avoid breaking changes; add new functions or v2 path.