Lesson 22 of 60 · Go
Dependency Management with Go Modules
Duration: 15 minutes
Go Modules Refresher
This lesson explains the main concept clearly with beginner-friendly examples and background so you can learn why the code works.
A module is a collection of related Go packages versioned together.
Initialising a module
$ mkdir cli && cd cli
$ go mod init github.com/yourname/cli
Adding a dependency
$ go get github.com/spf13/cobra@v1.7.0
The go.mod file now contains:
module github.com/yourname/cli
go 1.22
require github.com/spf13/cobra v1.7.0
Tidy up
$ go mod tidy // removes unused deps, adds missing ones
Vendor directory (optional)
$ go mod vendor
Versioning
- Tag releases with semantic versioning (
git tag v1.0.0). go install github.com/yourname/cli@latestfetches the latest version.