Adding the First Subcommand to the Application

Learn how to add the subcommand to our application by using the Cobra generator.

After initializing the application, we use the Cobra generator to add subcommands to it. The generator includes a file in the cmd directory for each subcommand. Each file includes boilerplate code for the subcommand. It also adds the subcommand to its parent, forming the tree-like structure.

Adding a subcommand

We add a new subcommand called hosts to our application to manage hosts in the hosts list. By default, Cobra adds this subcommand to the root command:

Get hands-on with 1200+ tech skills courses.