Build CLI applications with Cobra in Go
✓Works with OpenClaudeYou 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 versionto confirm Go 1.16+ is installed - Run
go get -u github.com/spf13/cobra/v2to install the latest Cobra version - Verify the Cobra CLI generator is available:
cobra-cli --help(install withgo install github.com/spf13/cobra-cli@latestif missing)
Steps
- Initialize a new Go project with
go mod init github.com/yourname/myappand create amain.gofile - Use
cobra-cli initin your project directory to scaffold the basic Cobra application structure - Generate new commands with
cobra-cli add commandnamefor each subcommand you need - Define command flags using
cmd.Flags().StringVar(),cmd.Flags().IntVar(), orcmd.Flags().BoolVar()in theinit()function of each command file - Mark required flags with
cmd.MarkFlagRequired("flagname")inside the command'sinit()function - Implement the command logic in the
RunorRunEfield—useRunEto return errors properly - Parse user input from
cmdargument usingcmd.Argsand access flag values through their bound variables - Build your CLI with
go build -o myappand 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
Related Go Skills
Other Claude Code skills in the same category — free to download.
Go API Setup
Scaffold Go REST API with Gin or Chi router
Go Testing
Set up Go testing with table-driven tests and mocks
Go Modules
Manage Go modules and dependency versioning
Go Concurrency
Implement goroutines, channels, and concurrency patterns
Go Docker
Create optimized multi-stage Docker build for Go apps
Go gRPC
Set up gRPC server and client in Go
Go Middleware
Create HTTP middleware chain in Go
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.