Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

module

go

People also ask

How do I update Go dependencies?

You can upgrade or downgrade a dependency module by using Go tools to discover available versions, then add a different version as a dependency. To discover new versions use the go list command as described in Discovering available updates.


Just 'go get' at the commit hash you want:

go get github.com/someone/some_module@af044c0995fe

'go get' will correctly update the dependency files (go.mod, go.sum).

More information: https://github.com/golang/go/wiki/Modules#how-to-upgrade-and-downgrade-dependencies


In addition the answer from Everton on using go get github.com/someone/some_module@af044c0995fe to get a specific commit, you can also use branch names such as:

  • go get github.com/someone/some_module@master
  • go get github.com/someone/some_module@dev_branch

Those examples get the latest commit on the corresponding branch.

It will still be recorded as a pseudo-version in your go.mod file, such as v0.0.0-20171006230638-a6e239ea1c69. (This helps provide a simple total ordering across all versions based on standard semver ordering).


If you want to temporarily substitute a dependency to a local directory (for example if you work on 2 modules sumultaneously) you can add replace statement at the end of go.mod file:

module example.com/mypkg

go 1.15

require (
  gitlab.com/someone/a_package v0.14.2
)

replace gitlab.com/someone/a_package => ../my_forks/a_package

I have been banging my head for some time that how it works for everyone and I am not able to run it. For me, I had to commit to master branch then only I was able to get it.

For go get to work with specific branch, commit id or tag, you need to enable a flag for go module by running below command

go env -w GO111MODULE=on

after this we will be able to do

go get repo@branchname
go get repo@tag
go get repo@commithash

Also if you put the word latest in place of the tag in the go.mod file it will get changed to the latest tag the modules.

For example:

module /my/module

require (
...
github.com/someone/some_module latest
...
)

will become

module /my/module

require (
...
github.com/someone/some_module v2.0.39
...
)

after running go mod tidy


  • Download source from branch
    go get your-repo@branch-name read output with go module version to be added to require or replace:
    go: downloading github.com/your-repo v1.2.3-0.20210609123456-123123123
  • Later this version can be found as output string of the following command
    go list -m -json your-repo@branch-name | jq '.|"\(.Path) \(.Version)"'
  • If jq is not installed on your PC - manually combine Path and Version values of result from:
    go list -m -json your-repo@branch-name
    separated by space:
    your-repository v1.2.3-0.20210609123456-123123123