golang-cli

Automate Go CLI development with Cobra and Viper patterns.

2.9k|191|Updated Mar 21, 2026
One-click install
npx skills add https://github.com/samber/cc-skills-golang --skill golang-cli
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-cli
Source: https://github.com/samber/cc-skills-golang/tree/main/skills/golang-cli
Command: npx skills add https://github.com/samber/cc-skills-golang --skill golang-cli

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Golang CLI projects often struggle with maintainability and consistency across commands, flags, configuration, and testing. This Skill provides a consolidated set of patterns and best practices to help you build production-ready command-line tools in Go using Cobra and Viper, with clear guidance on structure, I/O discipline, versioning, and automation.

Core Features & Use Cases

  • Recommended project layout for Cobra-based CLIs, including cmd/ structure and a minimal main.go that delegates to Execute().
  • Guidance on flag management, config layering with Viper, and environment variable handling.
  • Patterns for version embedding, exit codes, logging, shell completions, and test strategies for CLI commands.

Quick Start

Use this Skill to scaffold and audit a Cobra-based Go CLI by following the recommended layout and integrating Viper, versioning, and testing patterns.

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

Building a Go CLI with Cobra and Viper requires a dedicated cmd/ directory structure and a minimal main.go delegating to Execute(). This pattern ensures maintainable subcommands, persistent pre-run configuration, and clear separation of flag binding logic across complex command trees.

What is the best way to handle configuration layering in a Golang CLI?

The best way to handle configuration layering in a Golang CLI is binding Viper to Cobra flags. This approach natively manages environment variables, config files, and default flag values, ensuring proper precedence and persistent pre-run hooks for robust command execution.

How do I test Cobra commands in Go?

Testing Cobra commands in Go involves applying specific I/O and testing patterns to capture command output and validate execution. By structuring commands to accept injected writers and readers, you isolate dependencies and ensure reliable, automatable CLI test coverage.

Does this approach support version embedding for Go command-line tools?

Yes, this approach supports version embedding for Go command-line tools. By following these patterns, you can compile version metadata directly into your binary at build time, allowing the CLI to report its exact version through a dedicated subcommand or flag.

Can I use these patterns to audit an existing Golang CLI project?

Yes, you can use these patterns to audit an existing Golang CLI project. The guidelines cover root command setup, flag management, and I/O discipline, allowing you to review and refactor existing command-line tools for better maintainability and consistency.