golang-cli

Build Go CLI applications with Cobra and Viper command structures.

Updated Mar 8, 2026
One-click install
npx skills add https://github.com/tamago0224/kuroshio-mta --skill golang-cli-tamago0224
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-cli
Source: https://github.com/tamago0224/kuroshio-mta/tree/main/.agents/skills/golang-cli
Command: npx skills add https://github.com/tamago0224/kuroshio-mta --skill golang-cli-tamago0224

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes assets (resource) components.

What problem does it solve?

Many Go command-line tools suffer from inconsistent flag handling, brittle configuration layering, untestable I/O, improper signal handling, and brittle versioning. This Skill consolidates proven patterns and guardrails to build CLIs that behave predictably in automation, pipes, and production environments.

Core Features & Use Cases

  • Opinionated patterns: guidance for Cobra + Viper project layout, root command setup, PersistentPreRunE config initialization, and silence/error handling.
  • Operational concerns: flag binding to Viper, ldflags-based version embedding, stdout/stderr separation for pipe-friendly output, and signal.NotifyContext-based graceful shutdown.
  • Developer ergonomics: shell completion setup, RegisterFlagCompletionFunc examples, and unit testing patterns that capture command output for CI.
  • Use Case: scaffold a production-ready CLI with a minimal main, serve and version commands, robust config layering, and tests to validate output and exit codes.

Quick Start

Generate a minimal Cobra + Viper CLI scaffold with a root command, a serve subcommand, a version command using ldflags, and unit tests that capture stdout and stderr.

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 command and subcommand routing?

Structure a Go CLI using Cobra by defining a root command with subcommands, leveraging PersistentPreRunE for config initialization, and binding flags to Viper for deterministic configuration layering. This ensures predictable command routing and config handling.

How do I inject version info into a Go CLI during build using ldflags?

Inject version info into a Go CLI by passing variables via ldflags during the build process. This Skill guides embedding version details into specific variables, ensuring robust versioning without hardcoding values directly in the source code.

What is the best way to handle stdout and stderr separation in Go CLI tools?

Handle stdout and stderr separation by utilizing cmd.OutOrStdout() and ErrOrStderr() methods. This pattern ensures pipe-friendly output, directing standard data to stdout and error messages to stderr for reliable automation and scripting.

How do I implement graceful shutdown in a Go CLI with signal handling?

Implement graceful shutdown in a Go CLI by using signal.NotifyContext to intercept interrupt signals. This allows the application to clean up resources and exit safely, ensuring reliable behavior in production environments.

How do I write unit tests for Go CLI command output and exit codes?

Write unit tests for Go CLI commands by capturing stdout and stderr output during test execution. This Skill provides patterns to validate command output and exit codes, ensuring testable I/O and reliable continuous integration.

Does this Skill support adding shell completions for Cobra commands?

Yes, this Skill supports shell completions by providing guidance on setup and using RegisterFlagCompletionFunc examples. It helps enhance developer ergonomics by enabling automatic completion for commands and flags in compatible shell environments.