golang-cli

Build Go CLIs with Cobra and Viper using structured command and configuration patterns.

5|1|Updated Apr 14, 2026
One-click install
npx skills add https://github.com/omarluq/og-template --skill golang-cli-omarluq
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-cli
Source: https://github.com/omarluq/og-template/tree/main/.agents/skills/golang-cli
Command: npx skills add https://github.com/omarluq/og-template --skill golang-cli-omarluq

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Go developers often struggle to create maintainable, testable command-line interfaces. This skill provides a structured blueprint for building Go CLIs with Cobra and Viper, covering root and subcommands, flag binding, configuration layering, and testing patterns.

Core Features & Use Cases

  • Structured project layout: guides the typical go CLI layout (cmd/ with a minimal main.go) for predictable builds and testing.
  • Configuration layering: demonstrates binding flags to Viper, persistent vs local flags, and environment/config precedence.
  • Observability & testing: outlines standard patterns for tests, logging, and exit code handling in CLI tools.

Quick Start

Create a minimal Go CLI using Cobra and Viper with a root command and a sample subcommand, following the recommended project structure.

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 project with Cobra and Viper?

A structured Go CLI project uses a cmd/ directory with a minimal main.go entry point to separate command wiring from business logic. This layout ensures predictable builds, clear I/O semantics, and testable command behavior.

How does configuration layering work when binding flags in Viper?

Configuration layering in Viper binds command flags to establish a strict precedence across environment variables, config files, and local or persistent flags. This mechanism ensures predictable, environment-driven configuration for your Go CLI application.

What is the best way to test Cobra command behavior in Go?

Testing Cobra command behavior requires clear I/O semantics and strict exit code handling to verify command outputs accurately. Standard testing patterns isolate subcommand logic, ensuring predictable and maintainable test coverage for your CLI.

Can I use persistent flags versus local flags in a Cobra CLI?

Persistent flags apply globally to a parent command and all its subcommands, while local flags apply only to the specific command they define. Cobra wires these flag types distinctly to manage configuration layering.

How do I inject version information into a Go CLI during builds?

You inject version information into a Go CLI during builds using ldflags to set variables at compile time. This approach integrates with Cobra's root command setup to expose version details predictably.

How do I handle environment variables in a Cobra and Viper Go CLI?

Handling environment variables in a Cobra and Viper Go CLI involves binding flags to Viper to automatically map environment inputs into the configuration layer. This establishes proper precedence and ensures environment-driven configuration.