Search⌘ K

Working with Cobra - The Root Command

Understand how to define a root command with Cobra in Go, creating a foundation for command-line applications. This lesson guides you through setting up the root command, its role, and preparing your program for subcommands.

We'll cover the following...

Defining the root command

Our starting point is an application with a package for doing calculations. Here is the directory structure:

$ tree
.
├── go.mod
├── go.sum
└── pkg
    └── calc
        └── calc.go

As you can see, there is just the pkg directory with the calc package inside it. There isn’t even a main package yet.

The first step is ...