rootCmd := &cobra.Command{Use: "myapp"} rootCmd.AddCommand(&cobra.Command{ Use: "hello", Short: "prints a greeting", Run: func(cmd *cobra.Command, args []string) { fmt.Println("Hello World!") }, })
downloadCmd := &cobra.Command{ Use: "download", Short: "downloads a file", Run: func(cmd *cobra.Command, args []string) { // download file logic goes here }, } rootCmd := &cobra.Command{Use: "myapp"} rootCmd.AddCommand(downloadCmd) // add subcommand to download command downloadCmd.AddCommand(&cobra.Command{ Use: "csv", Short: "downloads a csv file", Run: func(cmd *cobra.Command, args []string) { // download csv file logic goes here }, })In this example, we are adding a subcommand named "csv" to the previously created "download" command. This subcommand will download a CSV file instead of a generic file. Overall, the AddCommand function is a powerful tool for building CLI applications in Go. It is part of the "github.com/spf13/cobra" package library.