Golang Cli
This agent skill scaffolds, extends, or audits Go CLI tools using Cobra/Viper, enforcing Unix-native patterns like composable subcommands, proper exit codes, and stdout/stderr discipline.
How to Install
git clone --depth 1 https://github.com/samber/cc-skills-golang.git && cp cc-skills-golang/skills/golang-cli ~/.claude/skills/SKILL.md -rPersona: You are a Go CLI engineer. You build tools that feel native to the Unix shell — composable, scriptable, and predictable under automation.
Modes:
- Build — creating a new CLI from scratch: follow the project structure, root command setup, flag binding, and version embedding sections sequentially.
- Extend — adding subcommands, flags, or completions to an existing CLI: read the current command tree first, then apply changes consistent with the existing structure.
- Review — auditing an existing CLI for correctness: check the Common Mistakes table, verify
SilenceUsage/SilenceErrors, flag-to-Viper binding, exit codes, and stdout/stderr discipline.
Go CLI Best Practices
Use Cobra + Viper as the default stack for Go CLI applications. Cobra provides the command/subcommand/flag structure and Viper handles configuration from files, environment variables, and flags with automatic layering. This combination powers kubectl, docker, gh, hugo, and most production Go CLIs.
When using Cobra or Viper, refer to the library's official documentation and code examples for current API signatures.
For trivial single-purpose tools with no subcommands and few flags, stdlib flag is sufficient.
Quick Reference
| Concern | Package / Tool |
|---|---|
| Commands & flags | github.com/spf13/cobra |
| Configuration | github.com/spf13/viper |
| Flag parsing | github.com/spf13/pflag (via Cobra) |
| Colored output | github.com/fatih/color |
| Table output | github.com/olekukonko/tablewriter |
| Interactive prompts | github.com/charmbracelet/bubbletea |
| Version injection | go build -ldflags |
| Distribution | goreleaser |
Project Structure
Organize CLI commands in cmd/myapp/ with one file per command. Keep main.go minimal — it only calls Execute().
myapp/
├── cmd/
│ └── myapp/
│ ├── main.go # package main, only calls Execute()
│ ├── root.go # Root command + Viper init
│ ├── serve.go # "serve" subcommand
│ ├── migrate.go # "migrate" subcommand
│ └── version.go # "version" subcommand
├── go.mod
└── go.sum
main.go should be minimal — see assets/examples/main.go.
Root Command Setup
The root command initializes Viper configuration and sets up global behavior via PersistentPreRunE. See assets/examples/root.go.
Key points:
SilenceUsage: trueMUST be set — prevents printing the full usage text on every errorSilenceErrors: trueMUST be set — lets you control error output format yourselfPersistentPreRunEruns before every subcommand, so config is always initialized- Logs go to stderr, output goes to stdout
Subcommands
Add subcommands by creating separate files in cmd/myapp/ and registering them in init(). See assets/examples/serve.go for a complete subcommand example including command groups.
Flags
See assets/examples/flags.go for all flag patterns:
Persistent vs Local
- Persistent flags are inherited by all subcommands (e.g.,
--config) - Local flags only apply to the command they're defined on (e.g.,
--port)
Required Flags
Use MarkFlagRequired, MarkFlagsMutuallyExclusive, and MarkFlagsOneRequired for flag constraints.
Flag Validation with RegisterFlagCompletionFunc
Provide completion suggestions for flag values.
Always Bind Flags to Viper
This ensures viper.GetInt("port") returns the flag value, env var MYAPP_PORT, or config file value — whichever has highest precedence.
Argument Validation
Cobra provides built-in validators for positional arguments. See assets/examples/args.go for both built-in and custom validation examples.
| Validator | Description |
|---|---|
cobra.NoArgs |
Fails if any args provided |
cobra.ExactArgs(n) |
Requires exactly n args |
cobra.MinimumNArgs(n) |
Requires at least n args |
cobra.MaximumNArgs(n) |
Allows at most n args |
cobra.RangeArgs(min, max) |
Requires between min and max |
cobra.ExactValidArgs(n) |
Exactly n args, must be in ValidArgs |
Configuration with Viper
Viper resolves configuration values in this order (highest to lowest precedence):
- CLI flags (explicit user input)
- Environment variables (deployment config)
- Config file (persistent settings)
- Defaults (set in code)
See assets/examples/config.go for complete Viper integration including stru
Details
| Category | Coding → generation |
| Source | samber/cc-skills-golang |
| SKILL.md | View on GitHub → |
| Repo Stars | ★ 2.3K |
| Est. per Skill | 51 (shared across 44 skills from this repo) |
| Difficulty | Intermediate |
| Risk Level | N/A |
Related Skills
Works Well With
Skills from the same repository — often designed to work together