Go Modules Dependency Workflow
Beginner8 min
Manage Go module dependencies including adding, updating, removing packages, and handling version conflicts.
Prerequisites
- -Go 1.21+ installed
- -An initialized Go module
Steps
1
Add a new dependency
Install a package and add it to go.mod automatically.
$ go get github.com/gin-gonic/gin@latest
Use @latest for the newest version, or @v1.9.0 to pin a specific version.
2
Tidy dependencies
Remove unused dependencies and add missing ones based on source code imports.
$ go mod tidy
3
List all dependencies
View the full dependency tree including indirect dependencies.
$ go list -m all
4
Check for available updates
See which dependencies have newer versions available.
$ go list -m -u all
Pipe through grep to filter: go list -m -u all | grep '\[.*\]'
5
Update a specific dependency
Upgrade a single dependency to its latest minor or patch version.
$ go get -u github.com/gin-gonic/gin && go mod tidy
The -u flag updates to the latest minor/patch. Use -u=patch to restrict to patch-level updates only.
Full Script
FAQ
Discussion
Loading comments...