CLI frameworks for Go (Golang): a complete overview of tools for creating console applications

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

CLI frameworks for Go (Golang): creating professional console applications



Go (Golang) is rightfully considered one of the best languages for creating console utilities. Thanks to fast compilation, built-in support for multithreading, and a convenient package system, legendary tools such as Docker, Kubernetes, Hugo, and Terraform are written in Go. However, to create a truly convenient and flexible CLI (Command Line Interface), you often need not only the standard flag library but also specialized frameworks. In this article, we will take a detailed look at the best CLI frameworks for Go, their capabilities, advantages, and usage examples.



Why do you need a CLI framework?



The standard flag library handles simple tasks well, but when your project grows, needs arise for:

  • Support for nested commands (subcommands), like in git commit or docker run.
  • Automatic generation of help and man pages.
  • Auto-completion (bash/zsh/fish completion).
  • Parsing complex flags and arguments.
  • Interactive mode (TUI).

CLI frameworks exist precisely for these tasks.



1. Cobra — the king of CLI in Go



Cobra is the most popular framework for creating CLI in Go. It is used in Kubernetes, Hugo, GitHub CLI, and many other projects. Cobra provides a powerful interface for creating commands, flags, and arguments.



Key features:

  • Easy creation of nested commands.
  • Automatic generation of help and man pages.
  • Support for auto-completion for bash, zsh, fish, and PowerShell.
  • Integration with Viper (configuration manager).
  • Project scaffolding generation via cobra-cli init.


Code example using Cobra:

package main

import ( "fmt" "os"

"github.com/spf13/cobra")

func main() { var rootCmd = &cobra.Command{ Use: "greet", Short: "Greeting utility", Long: "A simple utility for greeting a user", Run: func(cmd *cobra.Command, args []string) { name, _ := cmd.Flags().GetString("name") if name == "" { name = "world" } fmt.Printf("Hello, %s!", name) }, }

rootCmd.Flags().StringP("name", "n", "", "User name")

if err := rootCmd.Execute(); err != nil { fmt.Println(err) os.Exit(1) }}


Running this program with the --name "Vasya" flag will give you a greeting. Cobra will automatically add the --help flag and nice output formatting.



2. urfave/cli (formerly codegangsta/cli)



urfave/cli is the second most popular framework. It features a simple and intuitive API focused on declarative command description. It is well-suited for small and medium-sized projects.



Advantages:

  • Minimalistic and clear code.
  • Built-in support for flags, arguments, and subcommands.
  • Automatic help generation.
  • Support for actions before and after command execution (Before/After hooks).


Code example using urfave/cli:

package main

import ( "fmt" "log" "os"

"github.com/urfave/cli/v2")

func main() { app := &cli.App{ Name: "calculator", Usage: "Simple calculator", Commands: []*cli.Command{ { Name: "add", Aliases: []string{"a"}, Usage: "Add two numbers",

Recommendations