golang-cli

Create Go CLIs with Cobra and Viper for consistent commands and configuration.

7|1|Updated Mar 15, 2026
One-click install
npx skills add https://github.com/Harmeet10000/skills --skill golang-cli-harmeet10000
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-cli
Source: https://github.com/Harmeet10000/skills/tree/main/skills/backend/Golang/golang-cli
Command: npx skills add https://github.com/Harmeet10000/skills --skill golang-cli-harmeet10000

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Go CLI projects often suffer from inconsistent structure, configuration handling, and brittle testing. This skill provides a standardized approach to building, extending, and auditing CLIs in Go using Cobra and Viper, ensuring consistency across projects.

Core Features & Use Cases

  • Standardized Cobra-based project layout (root + subcommands, cmd/ directory)
  • Integrated Viper configuration with environment prefix, key replacer, and BindPFlag
  • Version embedding via ldflags for reliable release tagging
  • Safe I/O patterns and error handling (stdout vs stderr, SilenceUsage/Errors)
  • Shell completion, argument validation, and testable CLI patterns

Quick Start

Create a minimal Cobra + Viper CLI with a root command and a sample subcommand, wiring up a config flag and version info.

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 subcommands and configuration?

Structure a Go CLI with Cobra and Viper using a standardized cmd/ directory layout with a root command and subcommands, wiring Viper configuration via environment prefixes, key replacers, and BindPFlag for persistent flags.

What is the best way to handle environment-based configuration in a Golang command-line application?

Handle environment-based configuration in a Golang command-line application by integrating Viper with environment variable prefixes and key replacers, binding persistent flags directly to config keys using BindPFlag for consistent config loading.

How do I embed version information in a Go CLI during release?

Embed version information in a Go CLI during release by using ldflags to inject version variables at compile time, ensuring reliable release tagging and consistent version output across your command structure.

Can I implement shell completion and argument validation using Cobra in a Golang CLI?

Cobra supports implementing shell completion and argument validation in a Golang CLI through its built-in completion commands and argument validator functions, standardizing command input handling safely.

How do I write unit tests for a Cobra and Viper based Go command-line tool?

Write unit tests for a Cobra and Viper Go command-line tool by applying testable CLI patterns that separate command execution logic from I/O operations, utilizing safe I/O patterns and proper stdout versus stderr error handling.

Why does my Go CLI print usage on every runtime error, and how do I suppress it?

A Go CLI prints usage on runtime errors due to default Cobra behavior; suppress this by configuring SilenceUsage and SilenceErrors flags on your commands to ensure clean stderr error output without usage text.