Credits and Lineage
The decisions and memories in this template trace back to a handful of upstream sources. Everything is rewritten generically so the template drops into any Go CLI project without dragging upstream conventions or code along. Credit for the underlying ideas belongs to the authors below; mistakes and over-generalizations in the distillation belong to this repository.
github.com/cli/cli (the gh CLI)
Most of the architectural patterns in this template originate from the
gh CLI codebase. The gh team did the hard thinking for:
- Central
Factorystruct with lazy-closure dependencies injected into every command - The
Options+NewCmdXxx(f, runF)+ pure runFunc three-part command shape, including therunFtest-injection hook insideRunE - Semantic error types (
FlagError,SilentError,CancelError) mapped to distinct exit codes by a top-level runner IOStreamsabstraction wrapping In/Out/ErrOut with TTY detection and aNO_COLOR-awareColorScheme- Opt-in structured export via
--json/--jq/--templateas a first-class output mode - Cobra command groups for readable
--helporganization ErrHintwrapper for attaching user-facing remediation text to errors
Upstream: https://github.com/cli/cli
spf13/cobra
Most of this template uses cobra as its command substrate. Several
idioms in the cobra codebase reward explicit surfacing rather than
leaving agents to discover them the hard way:
- Ship shell completions via cobra's auto-generated
completion <shell>subcommand - Set
SilenceUsageandSilenceErrorson the root to stop cobra from dumping the usage blob on runtime errors PersistentPreRunEon the root command as app-wide middleware (auth, config load, logging init)- Generate reference docs (Markdown, man pages) from the cobra tree via
the
cobra/docpackage MarkFlagsMutuallyExclusive/MarkFlagsRequiredTogether/MarkFlagsOneRequiredas the declarative way to validate flag relationships (integrates with shell completion)
Upstream: https://github.com/spf13/cobra
Go source tree (standard library + cmd/go)
A second set of idioms comes directly from the Go source. The Go project
is one of the most idiomatic Go codebases in existence, and several
patterns are stdlib-endorsed or demonstrated by cmd/go itself:
signal.NotifyContextfor graceful Ctrl-C handling via context cancellationcontext.Contextthreaded through every runFunct.Helper(),t.Cleanup(),t.TempDir()for test ergonomicsfmt.Errorf("...: %w", err)as the canonical error-wrap verbfs.FS+fstest.MapFSas a testable filesystem seamflag.Value/pflag.Valuefor structured custom flag typessync.OnceValue[T]/sync.OnceValues[A, B]for lazy, type-safe memoization
Upstream: https://github.com/golang/go
Effective Go
https://go.dev/doc/effective_go is somewhat dated — some of its advice
(package-level init() functions, certain concurrency idioms) has aged
out of modern practice — but three stated conventions still match
current practice and are worth codifying:
- Accept interfaces, return concrete types
- Compile-time interface assertions with the blank identifier
(
var _ Iface = (*Concrete)(nil)) - Error messages: lowercase, no trailing punctuation, no newlines, so they compose cleanly under wrapping
Go Code Review Comments wiki
https://go.dev/wiki/CodeReviewComments is the community-maintained
list of style rules Go reviewers cite. Most of the always-on style
memories in this template (receiver-name, no-get-prefix,
doc-comment-shape, no-blank-error-discard, goroutine-exit-path,
context-first-param, pass-by-value-default, got-want-order,
errors-message-style, initialism-casing) are distillations of rules
stated there. The wiki is also the authoritative source for the
"avoid in-band error values" guidance behind byob-errors.5.
Google Go Style
https://google.github.io/styleguide/go/decisions is the public
Google Go style guide, structured as a set of "decisions" with
rationale (the same shape as the decisions/ tree in this template).
It restates and extends the Code Review Comments wiki; most memories
that cite the wiki cite this guide too. It is the source for the
quote-strings-in-errors rule and contributes to byob-errors.5 and
byob-testing.2.
Third-party libraries and tools
A few external libraries/tools are named in decisions where the
template picks a default implementation. Each See the … link goes
to the decision epic where the choice (and its swap-out story) is
spelled out in full.
charmbracelet/huh— prompter backend. See theprompterepic. Upstream: https://github.com/charmbracelet/huhcharmbracelet/bubbles— unknown-total progress spinner (viabubbles/spinner+bubbletea). See theprogressepic. Upstream: https://github.com/charmbracelet/bubblesschollz/progressbar/v3— known-total progress bar. See theprogressepic. Upstream: https://github.com/schollz/progressbargoreleaser/goreleaser— release pipeline (cross-compile matrix, archives, checksums). See thereleaseepic. Upstream: https://github.com/goreleaser/goreleaser