Command-line Interfaces (CLIs)

Overview

CLI developers prefer Go for portability, performance, and ease of creation

Command line interfaces (CLIs), unlike graphical user interfaces (GUIs), are text-only. Cloud and infrastructure applications are primarily CLI-based due to their easy automation and remote capabilities.

Key benefits

Leverage fast compile times to build programs that start quickly and run on any system

Developers of CLIs find Go to be ideal for designing their applications. Go compiles very quickly into a single binary, works across platforms with a consistent style, and brings a strong development community. From a single Windows or Mac laptop, developers can build a Go program for every one of the dozens of architectures and operating systems Go supports in a matter of seconds, no complicated build farms are needed. No other compiled language can be built as portably or quickly. Go applications are built into a single self contained binary making installing Go applications trivial.

Specifically, programs written in Go run on any system without requiring any existing libraries, runtimes, or dependencies. And programs written in Go have an immediate startup time—similar to C or C++ but unobtainable with other programming languages.

Use Case

Use Go for building elegant CLIs

When developing CLIs in Go, two tools are widely used: Cobra & Viper.

Cobra is both a library for creating powerful modern CLI applications and a program to generate applications and CLI applications in Go. Cobra powers most of the popular Go applications including CoreOS, Delve, Docker, Dropbox, Git Lfs, Hugo, Kubernetes, and many more. With integrated command help, autocomplete and documentation “[it] makes documenting each command really simple,” says Alex Ellis, founder of OpenFaaS.

Viper is a complete configuration solution for Go applications, designed to work within an app to handle configuration needs and formats. Cobra and Viper are designed to work together.

Viper supports nested structures in the configuration, allowing CLI developers to manage the configuration for multiple parts of a large application. Viper also provides all of the tooling need to easily build twelve factor apps.

“If you don’t want to pollute your command line, or if you’re working with sensitive data which you don’t want to show up in the history, it’s a good idea to work with environment variables. To do this, you can use Viper,” suggests Geudens.

Customer Brief introduction Projects using go
Comcast Comcast Comcast Comcast uses Go for a CLI client used to publish and subscribe to its high-traffic sites. The company also supports an open source client library which is written in Go - designed for working with Apache Pulsar.
GitHub GitHub GitHub GitHub uses Go for a command-line tool that makes it easier to work with GitHub, wrapping git in order to extend it with extra features and commands.
Hugo Hugo Hugo Hugo is one of the most popular Go CLI applications powering thousands of sites, including this one. One reason for its popularity is its ease of install thanks to Go. Hugo author Bjørn Erik Pedersen writes “The single binary takes most of the pain out of installation and upgrades.”

Get Started

Go books for creating CLIs

  • spf13/cobra

    A library for creating powerful modern CLI applications and a program to generate applications and CLI applications in Go

  • spf13/viper

    A complete configuration solution for Go applications, designed to work within an app to handle configuration needs and formats

  • urfave/cli

    A minimal framework for creating and organizing command line Go applications

  • delve

    A simple and powerful tool built for programmers used to using a source-level debugger in a compiled language

  • chzyer/readline

    A pure Golang implementation that provides most features in GNU Readline (under MIT license)

  • dixonwille/wmenu

    An easy-to-use menu structure for CLI applications that prompts users to make choices

  • spf13/pflag

    A drop-in replacement for Go’s flag package, implementing POSIX/GNU-style flags

  • golang/glog

    Leveled execution logs for Go

  • go-prompt

    A library for building powerful interactive prompts, making it easier to build cross-platform command line tools using Go.

View More