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

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 1:[1]

Just run go get <module>.

go get downloads the latest version of a dependency because that's what it does, not because you specified latest in go.mod.

is there a way to structure go.mod so that the latest version of a dependency is always used?

No, you are not supposed to manually edit the content of go.mod require directive yourself anyway. Moreover, the syntax of require directives is defined as:

require module-path module-version

where module-version can be an actual tagged version or a pseudo-version, e.g. when you require a specific commit.

Technically you can write latest in a require directive, but the next time you run a go command, it will replace the word latest with the actual latest (pseudo-)version tag. It will not stay latest, otherwise you wouldn't have deterministic dependencies.

Related: How to point Go module dependency in go.mod to a latest commit in a repo?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 blackgreen