package main import ( "fmt" "os" "github.com/spf13/cobra" ) var rootCmd = &cobra.Command{ Use: "myapp", Short: "My awesome CLI app", Long: `A longer description of my awesome CLI app`, Run: func(cmd *cobra.Command, args []string) { fmt.Println("Hello, world!") }, } func main() { if err := rootCmd.Execute(); err != nil { fmt.Println(err) os.Exit(1) } }
package main import ( "fmt" "os" "github.com/spf13/cobra" ) var rootCmd = &cobra.Command{ Use: "myapp", Short: "My awesome CLI app", Long: `A longer description of my awesome CLI app`, Run: func(cmd *cobra.Command, args []string) { fmt.Println("Hello, world!") }, } var subCmd = &cobra.Command{ Use: "subcommand", Short: "A subcommand", Long: `A longer description of the subcommand`, Run: func(cmd *cobra.Command, args []string) { fmt.Println("This is a subcommand") }, } func init() { rootCmd.AddCommand(subCmd) } func main() { if err := rootCmd.Execute(); err != nil { fmt.Println(err) os.Exit(1) } }In this example, we've added a subcommand to our CLI app using the `AddCommand` function. The `subCmd` variable defines the properties of the subcommand, including its `Short` and `Long` descriptions, and the function that is executed when the subcommand is run. Overall, go github.com/spf13.cobra provides a great way to build CLI applications in Go with minimal boilerplate code.