Using context.Context with cobra
Even if creating CLI applications is straighforward with Go, there are many libraries to help with that task, specially to handle subcommands and flags. Cobra is likely one of the more popular ones. It’s relatively simple to use and it’s easy to get something working very quickly.
A very basic example looks something like this:
rootCmd := &cobra.Command{ Use: "demo", Short: "demo shows how to pass a context.Context to a cobra command", Run: rootFn, } if err := rootCmd.