golang-cli

Create Go CLIs with Cobra and Viper supporting subcommands and configuration precedence.

Updated May 28, 2026
One-click install
npx skills add https://github.com/vanstinator/semantic-search --skill golang-cli-vanstinator
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-cli
Source: https://github.com/vanstinator/semantic-search/tree/main/.agents/skills/golang-cli
Command: npx skills add https://github.com/vanstinator/semantic-search --skill golang-cli-vanstinator

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes assets (resource) components.

What problem does it solve?

This Skill helps you design and implement Go command-line tools that behave predictably in automation, with correct flag handling, configuration layering, output discipline, and robust error/exit code behavior.

Core Features & Use Cases

  • Cobra-based CLI structure: command/subcommand organization, validators, and consistent registration patterns.
  • Viper configuration layering: precedence across flags, env vars, and config files with optional config support.
  • Production-grade CLI behavior: stdout/stderr separation, signal-aware graceful shutdown, shell completions, and ldflags version injection.

Quick Start

Use this Skill to generate a Cobra-based Go CLI named myapp with a root command, PersistentPreRunE configuration init, a serve subcommand, and properly bound flags to Viper.

Frequently Asked Questions about golang-cli

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I structure a Go CLI with Cobra and Viper for configuration layering?

Cobra and Viper structure Go CLIs by binding flags via viper.BindPFlag with env prefixing, initializing config in PersistentPreRunE, and layering precedence across flags, env vars, and config files.

Why does my Go CLI print usage on runtime errors instead of just the error message?

Your Go CLI prints usage on errors because SilenceUsage and SilenceErrors are unset. Setting these properties on your Cobra command stops usage output from cluttering stderr during runtime failures.

What is the correct way to handle stdout and stderr separation in a Cobra command?

Correct stdout and stderr separation in a Cobra command uses OutOrStdout for normal output and ErrOrStderr for error logging, ensuring predictable streams for automation and scripting.

How do I implement signal-aware graceful shutdown in a Go command-line tool?

Implement signal-aware graceful shutdown in a Go command-line tool using signal.NotifyContext to catch interrupt signals and cancel ongoing operations cleanly during termination.

Can I inject version information into a Go CLI during the build process?

Inject version information into a Go CLI during the build process using ldflags to pass version variables into the binary at compile time, making version data available to Cobra commands.

What is the best way to return Unix-convention exit codes from a Go CLI?

Return Unix-convention exit codes from a Go CLI by mapping command execution results to specific numeric codes, ensuring shells and automation pipelines correctly interpret success versus failure states.