$120 tested Claude codes · real before/after data · Full tier $15 one-timebuy --sheet=15 →
$Free 40-page Claude guide — setup, 120 prompt codes, MCP servers, AI agents. download --free →
clskills.sh — terminal v2.4 — 2,347 skills indexed● online
[CL]Skills_
GointermediateNew

Go CLI

Share

Build CLI applications with Cobra in Go

Works with OpenClaude

You are a Go developer building command-line applications. The user wants to create a CLI application using Cobra, a powerful framework for building modern CLI tools in Go.

What to check first

  • Run go version to confirm Go 1.16+ is installed
  • Run go get -u github.com/spf13/cobra/v2 to install the latest Cobra version
  • Verify the Cobra CLI generator is available: cobra-cli --help (install with go install github.com/spf13/cobra-cli@latest if missing)

Steps

  1. Initialize a new Go project with go mod init github.com/yourname/myapp and create a main.go file
  2. Use cobra-cli init in your project directory to scaffold the basic Cobra application structure
  3. Generate new commands with cobra-cli add commandname for each subcommand you need
  4. Define command flags using cmd.Flags().StringVar(), cmd.Flags().IntVar(), or cmd.Flags().BoolVar() in the init() function of each command file
  5. Mark required flags with cmd.MarkFlagRequired("flagname") inside the command's init() function
  6. Implement the command logic in the Run or RunE field—use RunE to return errors properly
  7. Parse user input from cmd argument using cmd.Args and access flag values through their bound variables
  8. Build your CLI with go build -o myapp and test with ./myapp command --flag value

Code

package main

import (
	"fmt"
	"log"

	"github.com/spf13/cobra"
)

var (
	name    string
	verbose bool
	count   int
)

var rootCmd = &cobra.Command{
	Use:   "myapp",
	Short: "A brief description of your application",
	Long:  `A longer description of what myapp does`,
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Println("myapp called")
	},
}

var greetCmd = &cobra.Command{
	Use:   "greet",
	Short: "Greet someone",
	Long:  `Greet a person by name, optionally multiple times`,
	RunE: func(cmd *cobra.Command, args []string) error {
		if name == "" {
			return fmt.Errorf("name flag is required")
		}
		for i := 0; i < count; i++ {
			fmt.Printf("Hello, %s!\n", name)
		}
		if verbose {
			fmt.Printf("Greeted %s %d times\n", name, count)
		}
		return nil
	},
}

var versionCmd = &cobra.Command{
	Use:   "version",
	Short: "Print version",
	Run: func(cmd *cobra

Note: this example was truncated in the source. See the GitHub repo for the latest full version.

Common Pitfalls

  • Treating this skill as a one-shot solution — most workflows need iteration and verification
  • Skipping the verification steps — you don't know it worked until you measure
  • Applying this skill without understanding the underlying problem — read the related docs first

When NOT to Use This Skill

  • When a simpler manual approach would take less than 10 minutes
  • On critical production systems without testing in staging first
  • When you don't have permission or authorization to make these changes

How to Verify It Worked

  • Run the verification steps documented above
  • Compare the output against your expected baseline
  • Check logs for any warnings or errors — silent failures are the worst kind

Production Considerations

  • Test in staging before deploying to production
  • Have a rollback plan — every change should be reversible
  • Monitor the affected systems for at least 24 hours after the change

Quick Info

CategoryGo
Difficultyintermediate
Version1.0.0
AuthorClaude Skills Hub
goclicobra

Install command:

curl -o ~/.claude/skills/go-cli.md https://claude-skills-hub.vercel.app/skills/go/go-cli.md

Related Go Skills

Other Claude Code skills in the same category — free to download.

Want a Go skill personalized to YOUR project?

This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.