Golang Cobra

Learn via video courses
Topics Covered

Overview

Cobra is one of the most powerful command line tool packages in the Go language. It is generally used with projects that use Kubernetes, GitHub CLI, and Hugo. It is known as a CLI framework for Golang.

Introduction to Cobra in Golang

Cobra is a framework and a tool that is used by Golang quite extensively for creating command line interfaces also known as CLI-based applications. It does this by providing a wide range of tools that are used for automating the process of application and to provide a key abstraction that is used for increasing the productivity of a developer.

How to Use Cobra in Golang

The Cobra can be used in Golang by installing the required dependencies and modules. Make sure that the version of the Cobra that you install is the latest and updated one.

Why Use Cobra in Golang

There are several benefits of using Cobra with golang, a few of which are as follows:

  • It provides help generation commands and flags that are automatic
  • By using Cobra with golang, subcommand-based CLIs can be created easier which then uses nested subcommands
  • It helps provide smart suggestions and automatic auto-correct
  • It is used for increasing productivity because of using commands like Cobra add cmdname and Cobra init appname

Installing Golang Cobra

In order to install Cobra for using it with Golang, use the command ‘go get’ for installing the latest version of the Cobra library. By executing this command in your CLI, the executable file of the Cobra will get installed along with different libraries and dependencies. Look at the command given below to understand how to use install Cobra:

Once the Cobra library is installed, use it by including it in the project as given below:

Using Golang Cobra

In order to use the Cobra library that you just installed in your project, initialize the Cobra CLI application with the command:

By using this command, the basic application structure will be created for using it with your project. We can also add a command to the application or project by using the syntax:

Note: by using the snake_case/snake-case against the Cobra library can cause an error and hence try using camelCase instead of any other case. Once this is done, the project directory will look something like this:

Implementation of Cobra in Golang

In this section, we will learn about the easy use case of the Cobra. In the code snippet given below, a simple CLI application is created for making additions. In order to implement this, we will be needing 3 files as shown in the table given below:

NumberFile nameFile directory
1main.goapp/demo
2root.goapp/demo/cmd
3add.goapp/demo/cmd

For initializing the CLI application, we will be using the following command in the terminal:

This command will create the file main.go as well as root.go. The file main.go is the entry point for the Cobra application and its structure of it is as given below:

We can now insert the ‘add’ command by using the command as shown below: cobra add add The above command, when executed, will create a file for the ‘add’ command. The code for the same file is as shown below:

The program can now be executed by running the commandas shown below:

Once the above command is executed, execute the next command which is as shown below:

Once these commands are executed, the output will be displayed as shown below:

Examples for Understanding

In order to understand this more clearly, let's take an example of adding even numbers from the list of numbers. For this example, open the command line terminal in the newly created directory and executed the command given below:

Te command even gets added in the file named even.go which is added in cmd folder. Copy the code given below in the file even.go

In this example, the first step is that the strings get converted to int by using the package strconv which then checks if the number is even or not and then adds it to the result. Save the changes made in the file and build it again by using the command shown below:

Once this is installed, run the command given below to execute the program.

Output:

Conclusion

  • Cobra is one of the most powerful command line tool packages in the Go language. It is generally used with projects that use Kubernetes, GitHub CLI, and Hugo. It is known as a CLI framework for golang.
  • Cobra is a framework and a tool that is used by Golang quite extensively for creating command line interfaces also known as CLI-based applications. It does this by providing a wide range of tools that are used for automating the process of application and to provide a key abstraction that is used for increasing the productivity of a developer.
  • Command to install Cobra:
  • Once the Cobra library is installed, use it by including it in the project as given below: