Use go get -u to update all dependencies to their latest compatible versions, or go get <module>@latest to target a specific package. After updating, always run go mod tidy to clean up the go.mod and go.sum files, removing unused packages and ensuring the module graph is consistent.
For a full project update, run these commands in your module root:
# Update all dependencies to the latest compatible versions
go get -u ./...
# Clean up go.mod and go.sum
go mod tidy
If you need to update a specific dependency to a version newer than what go get -u resolves (which respects semantic versioning constraints), specify the module and version explicitly:
# Update a specific package to its latest version, ignoring constraints
go get github.com/gin-gonic/gin@latest
# Or update to a specific version
go get github.com/gin-gonic/gin@v1.9.1
# Clean up again to ensure consistency
go mod tidy
Be aware that go get -u will only bump versions within the same major version range defined in your go.mod (e.g., v1.2.0 to v1.9.0) to prevent breaking changes. If you need to jump major versions (e.g., v1 to v2), you must explicitly request the new major version using the @v2 suffix or the specific version tag, as Go treats major version changes as distinct modules.
Always verify the changes by running your tests immediately after updating:
go test ./...
If the update introduces breaking changes, you may need to adjust your code or revert the specific dependency using go get <module>@<previous-version> before running go mod tidy again. This workflow ensures your dependency graph remains valid and your application builds correctly.