July 16, 2020
Bootstrap a Go Cobra application with Go Modules
To bootstrap a Go application using the Cobra generator and Go Modules, start by installing the Cobra CLI:
# Install Cobra
go get github.com/spf13/cobra/cobraInitialize your Cobra project, but remember to use your package’s fully qualified name, as in: github.com/laurybueno/kubectl-hoggers. Don’t worry if your package is not publicly available yet. And there is no need to put your source code inside GOPATH anymore (since Go 1.11, at least):
cobra init --pkg-name <fully_qualified_package_name>Initialize your new module:
go mod init <fully_qualified_package_name>Fetch all dependencies and install it:
go get -d -v ./...
go install -v ./...Your app’s command line is now ready to be used:
kubectl-hoggers