July 16, 2020
Bootstrap a Go Cobra application with Go Modules
To bootstrap a Go application using Cobra Generator and Go Modules, start by installing the Cobra command line:
# Install Cobra
go get github.com/spf13/cobra/cobra
Initialize 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>
Get recursively all its dependencies and install it:
go get -d -v ./...
go install -v ./...
Your app’s command line is now ready to be used:
kubectl-hoggers
Further reading
Docker and Go: https://www.docker.com/blog/docker-golang/
Go Modules: https://github.com/golang/go/wiki/Modules