golang-cli

Develop Go CLI behavior with Cobra and Viper for automation.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

It helps you design and implement Go command-line tools that behave correctly in automation, including consistent command structure, configuration layering, output discipline, completions, and error/exit-code handling.

Core Features & Use Cases

  • Go CLI engineering guidance (Cobra/Viper-first): Covers command trees, persistent vs local flags, flag constraints, and binding flags to configuration via Viper.
  • Production-quality UX for shells & scripts: Establishes stdout/stderr separation, machine-readable output patterns, and shell completion setup.
  • Reliability under automation: Provides patterns for version injection with ldflags, signal handling with context cancellation, Unix exit code conventions, and CLI testing practices.

Use cases include building a new Go CLI, extending an existing Cobra CLI with subcommands/flags/completions, and reviewing an existing CLI for correctness against common pitfalls.

Quick Start

Use this skill when you are creating a new Go CLI command tree with Cobra and want consistent configuration via Viper, correct stdout/stderr behavior, and proper exit codes under failure conditions.

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 predictable automation?

Structure a Go CLI using Cobra command trees with persistent and local flags, binding configuration via Viper for layered config. This ensures predictable command execution, flag validation, and correct automation behavior.

What is the correct way to handle stdout and stderr separation in Go command-line tools?

Correct stdout and stderr separation in Go CLIs requires strict output discipline, routing operational data to stdout and diagnostic messages to stderr. This pattern supports machine-readable output and reliable shell scripting.

How do I implement Unix exit codes and signal handling in a Go CLI?

Implement Unix exit codes and signal handling in a Go CLI by using context cancellation for graceful shutdowns. Enforcing conventional exit codes under failure conditions ensures your tool behaves reliably within automation pipelines.

How do I set up shell completion for an existing Cobra CLI in Go?

Set up shell completion for a Cobra CLI by extending the command tree with Cobra's built-in completion generation. This provides users with automatic command and flag suggestions, improving the interactive shell experience.

What is the best way to inject version information into a Go binary during build?

Inject version information into a Go binary during build using ldflags. This approach embeds version metadata directly into the CLI, allowing the application to report its exact version during execution or troubleshooting.

Why does my Go CLI fail when used in shell scripts despite correct logic?

A Go CLI fails in shell scripts if it lacks output discipline, proper exit codes, or stdout/stderr separation. Reviewing command execution patterns and enforcing strict output routing resolves automation pipeline conflicts.