4 Must-Have Golang Packages for Building Powerful CLI Apps

Golang is preferred for developing CLI applications because of its portability, performance, and ease of creation. Golang CLI applications are compiled fast that starts quickly. Golang programs can run on any system without needing any existing libraries, runtimes, and dependencies. There are many big enterprises using Golang for CLI, like the GitHub command line tool, Kubernetes, stripes CLI, Uber CLI, and many more.

In this blog, you are going to see the list of Golang packages used for building powerful modern CLI. After that, you will see a full project built using these packages. Let’s roll.

team, cooperation, planning-4503157.jpg

Packages

There are many Golang libraries that make your CLI development easy. Together they can take your CLI to the next level.

  1. Cobra: A library for creating powerful modern CLI applications and a program to generate applications and CLI applications in Go. I have already written a blog to build your own modern powerful cli application with Cobra.
  2. Viper: A complete configuration solution for Go applications. It can handle all configurations formats and needs. Viper can help you read config from JSON, TOML, YAML, HCL, envfile, and Java properties config files. It can also help in reading from environment variables.
  3. Progress bar: A very simple thread-safe progress bar. It can be used for I/O operations and operations with unknown lengths too. You can customize this progress bar like change the writer, the color, the width, the description, the theme, etc.
  4. Color: Color lets you use colorized outputs in terms of ANSI Escape Codes in Go (Golang).

Example

Let’s use these packages to create a real example. I have created a repo cassandra-golang-count. This can do two tasks.

  1. the sum of N natural numbers
  2. count from Cassandra’s table.

This example will help you to understand the real-time use of these packages.

The first example, it returns the sum of N natural numbers. Here cobra is used for making CLI applications. To illustrate the progress bar I have used sleep for 40 milliseconds. And output, i.e the sum of n natural numbers is shown in Magenta color with the help of the Color package. This is a simple-to-understand example.

Progress Bar

The second example is the one which can be used to solve the real problem of Cassandra count. Cassandra count is expensive and for large datasets, it can give you a timeout. To solve this I have used pagination to fetch data from the table and keep count in memory. In this example, the viper is used to get the Cassandra configuration from the config file. This can help you to keep separate configs for different environments.

Output for Cassandra Count

Disclaimer: Please use this Cassandra count in production after proper testing. I have tested for more than 1 million rows, and it worked fine for me in production.

Conclusion:

Go is a great choice for writing your next CLI app, and these packages can ease the process. These packages can make your next CLI powerful and modern. Do build some CLI with these packages and let me know your experience. Also, comment if you are using some other packages for making CLI in Golang.

For more such content, consider subscribing blogyourcode.

Happy Coding!!!

Leave a Reply