Find version of a module

35.6k Views Asked by At

We are working with Go modules. I want in a CLI to get the specific version of a module. Is it possible?

If you are curious, the reason is that I want to add the following generate command:

//go:generate go run github.com/golang/mock/mockgen -source="$GOPATH/pkg/mod/mymodules.com/mymodule@${VERSION}/module.go" -destination=module_mock.go

So I need to somehow get the version

3

There are 3 best solutions below

1
On BEST ANSWER

Basics:

go list -m all — View final versions that will be used in a build for all direct and indirect dependencies

go list -u -m all — View available minor and patch upgrades for all direct and indirect dependencies

Example:

To get the version of a specific module, let's say golang.org/x/text

go list -m all | grep golang.org/x/text | awk '{print $2}'

or

go list -u -m all | grep golang.org/x/text | awk '{print $2}'

So, the general way:

go list -u -m all | grep <module-name> | awk '{print $2}'
1
On

Late but worth to mention, if you want to check the all available versions of a specific module:

go list -m -versions <module_name>

Like:

go list -m -versions github.com/user/module-name

0
On

This command will get the version of go that's used in a project.

[$]> grep -m 1 go go.mod | cut -d\ -f2

That'll output a version like 1.18.