find module of dependent package

50 views
Skip to first unread message

Dan Kortschak

unread,
Dec 23, 2019, 6:23:30 PM12/23/19
to golang-nuts
Say I have a package path, "host.org/user/depmodule/pkgdep", which is
the path to a dependency of "host.org/user/repomain/pkgmain". The
go.mod file in host.org/user/repomain will have a line
"host.org/user/depmodule" in the require block. If I want to
programmatically find what the module path is from the package path
"host.org/user/depmodule/pkgdep", how can I do this?

go list -m only works with module paths, so doing `go list -m
host.org/user/depmodule/pkgdep' when I'm in
host.org/user/repomain/pkgmain gives me:

go list -m: module host.org/user/depmodule/pkgdep: not a known dependency

Is there a way I can use go list to get this information, or a helper
function somewhere that does this? (The go tool must have this capacity
since go get depends on it when grabbing dependencies).

thanks
Dan

Dan Kortschak

unread,
Dec 23, 2019, 11:21:55 PM12/23/19
to golang-nuts
Solved thanks to Daniel Martí; use `go list -json` and grab the Module
struct out of that.
Reply all
Reply to author
Forward
0 new messages