How to Add Subcommands to a Go CLI with Cobra

Cli
Add subcommands to a Cobra CLI by defining a new Command struct and appending it to the parent command's Commands slice.

Add subcommands to a Cobra CLI by creating a new command struct, defining its usage and run function, and appending it to the parent command's Commands slice.

var cmdRun = &cobra.Command{
	Use:   "run",
	Short: "Run the application",
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Println("Running...")
	},
}

func init() {
	rootCmd.AddCommand(cmdRun)
}