Grokify
Grokify

Reputation: 16354

How to get a list of Go dependents

Short of building a dependency graph, is there an existing way to retrieve a list of dependent packages given a target package?

Sourcegraph has a badge (shield) that shows you how many packages use a specific package but when I click their link to go to their UI, I cannot find a count or list of packages. Here's more info:

Example badge:

Sourcegraph

GitHub has a dependency graph but it doesn't appear to list packages for Go and its docs only mention JavaScript and Ruby per the following:

Upvotes: 2

Views: 1342

Answers (2)

dolmen
dolmen

Reputation: 8706

pkg.go.dev (provided by the Go team at Google) has that feature. Check the imported by tab.

Example: https://pkg.go.dev/github.com/dolmen-go/jsonptr?tab=importedby

deps.dev (also provided by Google) also has that feature for Go packages and also for other programming ecosystems.

Example: https://deps.dev/go/github.com%2Fdolmen-go%2Fjsonptr/v0.0.0-20200427210345-20e1608f9d85/dependents

Upvotes: 0

Jonathan Hall
Jonathan Hall

Reputation: 79744

go list can do this for you, with use of the -f flag.

go list -f '{{.Imports}}' ./...

Will show a list of all imports for the current and child directories. You can pipe this through sort -u, for example, to get a list of all dependencies, or do other standard shell-based massaging. Consult the documentation for all of the formatting options available.

If you need a recursive list, this is also possible with some shell scripting, by then using doing the same thing recursively on the output of the above command.

Upvotes: 5

Related Questions