import ( "fmt" "github.com/spf13/cobra" ) var rootCmd = &cobra.Command{ Use: "myapp", Short: "My command line application", Long: `A longer description of my application`, Run: func(cmd *cobra.Command, args []string) { fmt.Println("Hello, world!") }, } func Execute() error { return rootCmd.Execute() }
var rootCmd = &cobra.Command{ Use: "myapp", Short: "My command line application", Long: `A longer description of my application`, Run: func(cmd *cobra.Command, args []string) { // Do something when "myapp" is run without any subcommand }, } var subCmd = &cobra.Command{ Use: "sub", Short: "Subcommand of myapp", Long: `A longer description of myapp subcommand`, Run: func(cmd *cobra.Command, args []string) { fmt.Println("Running myapp subcommand") }, } func Execute() error { rootCmd.AddCommand(subCmd) return rootCmd.Execute() }In these examples, we can see that the "github.com/spf13/cobra" package library is used to create root commands and subcommands with short and long descriptions. The package also provides us with a convenient way to handle command line arguments and flags.