golang-cli▌
samber/cc-skills-golang · updated Apr 8, 2026
MDX-style export adds YAML metadata + attribution linking explainx.ai and this canonical listing URL.
Persona: You are a Go CLI engineer. You build tools that feel native to the Unix shell — composable, scriptable, and predictable under automation.
Persona: 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 struct unmarshaling and config file watching.
Example Config File (.myapp.yaml)
port: 8080
host: localhost
log-level: info
database:
dsn: postgres://localhost:5432/myapp
max-conn: 25
With the setup above, these are all equivalent:
- Flag:
--port 9090 - Env var:
MYAPP_PORT=9090 - Config file:
port: 9090
Version and Build Info
Version SHOULD be embedded at compile time using ldflags. See assets/examples/version.go for the version command and build instructions.
Exit Codes
Exit codes MUST follow Unix conventions:
| Code | Meaning | When to Use |
|---|---|---|
| 0 | Success | Operation completed normally |
| 1 | General error | Runtime failure |
| 2 | Usage error | Invalid flags or arguments |
| 64-78 | BSD sysexits | Specific error categories |
| 126 | Cannot execute | Permission denied |
| 127 | Command not found | Missing dependency |
| 128+N | Signal N | Terminated by signal (e.g., 130 = SIGINT) |
See assets/examples/exit_codes.go for a pattern mapping errors to exit codes.
I/O Patterns
See assets/examples/output.go for all I/O patterns:
- stdout vs stderr: NEVER write diagnostic output to stdout — stdout is for program output (pipeable), stderr for logs/errors/diagnostics
- Detecting pipe vs terminal: check
os.ModeCharDeviceon stdout - Machine-readable output: support
--outputflag for table/json/plain formats - Colors: use
fatih/colorwhich auto-disables when output is not a terminal
Signal Handling
Signal handling MUST use signal.NotifyContext to propagate cancellation through context. See assets/examples/signal.go for graceful HTTP server shutdown.
Shell Completions
Cobra generates completions for bash, zsh, fish, and PowerShell automatically. See assets/examples/completion.go for both the completion command and custom flag/argument completions.
Testing CLI Commands
Test commands by executing them programmatically and capturing output. See assets/examples/cli_test.go.
Use cmd.OutOrStdout() and cmd.ErrOrStderr() in commands (instead of os.Stdout / os.Stderr) so output can be captured in tests.
Common Mistakes
| Mistake | Fix |
|---|---|
Writing to os.Stdout directly |
Tests can't capture output. Use cmd.OutOrStdout() which tests can redirect to a buffer |
Calling os.Exit() inside RunE |
Cobra's error handling, deferred functions, and cleanup code never run. Return an error, let main() decide |
| Not binding flags to Viper | Flags won't be configurable via env/config. Call viper.BindPFlag for every configurable flag |
Missing viper.SetEnvPrefix |
PORT collides with other tools. Use a prefix (MYAPP_PORT) to namespace env vars |
| Logging to stdout | Unix pipes chain stdout — logs corrupt the data stream for the next program. Logs go to stderr |
| Printing usage on every error | Full help text on every error is noise. Set SilenceUsage: true, save full usage for --help |
| Config file required | Users without a config file get a crash. Ignore viper.ConfigFileNotFoundError — config should be optional |
Not using PersistentPreRunE |
Config initialization must happen before any subcommand. Use root's PersistentPreRunE |
| Hardcoded version string | Version gets out of sync with tags. Inject via ldflags at build time from git tags |
Not supporting --output format |
Scripts can't parse human-readable output. Add JSON/table/plain for machine consumption |
Related Skills
See samber/cc-skills-golang@golang-project-layout, samber/cc-skills-golang@golang-dependency-injection, samber/cc-skills-golang@golang-testing, samber/cc-skills-golang@golang-design-patterns skills.
How to use golang-cli on Cursor
AI-first code editor with Composer
Prerequisites
Before installing skills in Cursor, ensure your development environment meets these requirements:
- ›Cursor installed and configured on your development machine
- ›Node.js version 16.0+ with npm package manager (verify with
node --version) - ›Active project directory or workspace where you want to add golang-cli
Execute installation command
Execute the skills CLI command in your project's root directory to begin installation:
The skills CLI fetches golang-cli from GitHub repository samber/cc-skills-golang and configures it for Cursor.
Select Cursor when prompted
The CLI will show a list of available agents. Use arrow keys to navigate and space to select Cursor:
Verify installation
Confirm successful installation by checking the skill directory location:
Reload or restart Cursor to activate golang-cli. Access the skill through slash commands (e.g., /golang-cli) or your agent's skill management interface.
Security & Verification Notice
We perform automated surface-level scans (Gen AI Scanner, Socket, Snyk) during installation. These checks detect common vulnerabilities but do not guarantee complete security. Always review skill source code and verify the publisher's reputation before production use.
Skills execute code in your development environment. Always verify the publisher's identity, review recent commits, and test in isolated environments before production deployment.
List & Monetize Your Skill
Submit your Claude Code skill and start earning
Use Cases▌
Task Automation & Efficiency
Automate repetitive workflows and reduce manual effort
Example
Generate reports, summarize documents, draft communications
Save 3-5 hours per week on routine tasks
Knowledge Enhancement
Learn new skills, understand complex topics, get expert guidance
Example
Explain concepts, provide examples, suggest learning resources
Accelerate learning and skill development by 2x
Quality Improvement
Enhance output quality through reviews, suggestions, and refinements
Example
Review drafts, suggest improvements, catch errors
Improve work quality by 30-40% with less effort
Implementation Guide▌
Prerequisites
- ›Claude Desktop or compatible AI client with skill support
- ›Clear understanding of task or problem to solve
- ›Willingness to iterate and refine outputs
Time Estimate
15-45 minutes depending on use case complexity
Installation Steps
- 1.Install skill using provided installation command
- 2.Test with simple use case relevant to your work
- 3.Evaluate output quality and relevance
- 4.Iterate on prompts to improve results
- 5.Integrate into regular workflow if valuable
Common Pitfalls
- ⚠Expecting perfect results without iteration
- ⚠Not providing enough context in prompts
- ⚠Using skill for tasks outside its intended scope
- ⚠Accepting outputs without review and validation
Best Practices▌
✓ Do
- +Start with clear, specific prompts
- +Provide relevant context and constraints
- +Review and refine all outputs before using
- +Iterate to improve output quality
- +Document successful prompt patterns
✗ Don't
- −Don't use without understanding skill limitations
- −Don't skip validation of outputs
- −Don't share sensitive information in prompts
- −Don't expect skill to replace human judgment
💡 Pro Tips
- ★Be specific about desired format and style
- ★Ask for multiple options to choose from
- ★Request explanations to understand reasoning
- ★Combine AI efficiency with human expertise
When to Use This▌
✓ Use When
Use when skill capabilities match your task, clear ROI on time saved, and you can validate outputs. Best for repetitive tasks, learning, and quality improvement.
✗ Avoid When
Avoid when task requires deep expertise you can't validate, involves sensitive decisions, or when learning process is more valuable than speed of completion.
Learning Path▌
- 1Familiarize yourself with skill capabilities and limitations
- 2Start with low-risk, non-critical tasks
- 3Progress to more complex and valuable use cases
- 4Build expertise through regular use and experimentation
Discussion
Product Hunt–style comments (not star reviews)- No comments yet — start the thread.
Ratings
4.5★★★★★57 reviews- ★★★★★Ava Iyer· Dec 20, 2024
We added golang-cli from the explainx registry; install was straightforward and the SKILL.md answered most questions upfront.
- ★★★★★Aditi Choi· Dec 20, 2024
golang-cli is among the better-maintained entries we tried; worth keeping pinned for repeat workflows.
- ★★★★★Aditi Lopez· Dec 16, 2024
I recommend golang-cli for anyone iterating fast on agent tooling; clear intent and a small, reviewable surface area.
- ★★★★★Aditi Agarwal· Dec 16, 2024
Registry listing for golang-cli matched our evaluation — installs cleanly and behaves as described in the markdown.
- ★★★★★Kaira Iyer· Dec 12, 2024
golang-cli reduced setup friction for our internal harness; good balance of opinion and flexibility.
- ★★★★★Kaira Gupta· Dec 4, 2024
Useful defaults in golang-cli — fewer surprises than typical one-off scripts, and it plays nicely with `npx skills` flows.
- ★★★★★Ava Menon· Nov 11, 2024
golang-cli reduced setup friction for our internal harness; good balance of opinion and flexibility.
- ★★★★★Aditi Abbas· Nov 11, 2024
Solid pick for teams standardizing on skills: golang-cli is focused, and the summary matches what you get after install.
- ★★★★★Aditi Park· Nov 7, 2024
golang-cli fits our agent workflows well — practical, well scoped, and easy to wire into existing repos.
- ★★★★★William Martin· Nov 7, 2024
I recommend golang-cli for anyone iterating fast on agent tooling; clear intent and a small, reviewable surface area.
showing 1-10 of 57