Search code examples
godependenciesgo-modules

How to get all dependencies (modules) used?


I try to get all the dependencies my project uses.

I looked at the go.mod file, though this only contains dependencies/modules that I added, and not the dependencies from my dependencies.

Looking at the go.sum file, this looked more promising, though then I noticed it contains multiple duplicates. Even though I only use one version of the dependency. As an example:

github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=

How can I get a list containing only all actively used dependencies/modules?


Solution

  • You are right about these 2 files, and technically you can clean up the go.sum by adding replace statements to the go.mod file. Though, it is not reliable that you (and your team) will always keep this clean.

    In my opinion, the best way to get the list you are looking for is go list -m all (or go list --json -m all to get a json response). It will list you all the dependencies recursively in your project. But filters out the unused ones.