~/Golang CLI Tools Overview

Jul 8, 2019


Golang excels at building command-line interface tools CLI due to its static binary generation, speed, and ease of deployment.

Popular CLI Tool Libraries

Basic Example Using Cobra

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
package main

import (
    "fmt"
    "github.com/spf13/cobra"
)

func main() {
    var rootCmd = &cobra.Command{
        Use:   "example",
        Short: "A simple example CLI",
        Run: func(cmd *cobra.Command, args []string) {
            fmt.Println("Hello CLI")
        },
    }
    rootCmd.Execute()
}

Useful Features

Benefits

Golang compiles to static binaries ideal for distribution. Its strong concurrency model and easy cross-compilation help for performant and portable CLI tools.

Further reading: Effective Go: Command Line.

Tags: [golang] [cli]