Always require the latest version of a dependency in go.mod

Issue

Leaving aside whether this is a good idea, is there a way to structure go.mod so that the latest version of a dependency is always used?

The one way that I have discovered to do it is to, for example, have

require (
    gonum.org/v1/gonum latest
)

which downloads and resolves to the latest version of gonum when using e.g. go get. However, that also updates my go.mod file, removing the latest tag.

Should I just keep the go.mod file in my git repository as a version containing the latest tag and allow users’ versions to be updated when they build etc.?

Solution

There’s no way to do this automatically in go.mod. That’s by design actually: go.mod is intended to let the go command select a set of versions deterministically with any build. If the go command always picked the latest version of a dependency, the set of selected versions would change over time without any user action. If one of your dependencies always picked the latest version of one its dependencies, that could break your build, and it would be difficult to override.

go get example.com/mod is the best way to stay up to date. That needs to be done manually, but you can automate it with a script or an action in CI if you have a large number of dependencies.

Answered By – Jay Conrod

Answer Checked By – David Marino (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.