How to list installed go packages

go list ... is quite useful, but there were two possible issues with it for me:

  1. It will list all packages including standard library packages. There is no way to get the explicitly installed packages only (which is what I assume the more interesting inquiry).
  2. A lot of times I need only the packages used in my projects (i.e. those listed in the respective go.mod files), and I don't care about other packages lying around (which may have been installed just to try them out). go list ... doesn't help with that.

So here's a somewhat different take. Assuming all projects are under ~/work:

find ~/work -type f -name go.mod \
-exec sed $'/^require ($/,/^)$/!d; /^require ($/d;/^)$/d; /\\/\\/ indirect$/d; s/^\t+//g' {} \; \
| cut -d' ' -f1 \
| sort | uniq

A line by line explanation:

  1. find all go.mod files
  2. apply sed to each file to filter its content as follows (explained expression by expression):
    • extract just the require( ... ) chunks
    • remove the require( and ) lines, so just lines with packages remain
    • remove all indirect packages
    • remove leading tabs 1)
  3. extract just the qualified package name (drop version information)
  4. remove duplicate package names

1) Note the sed expression argument uses bash quoting to escape the TAB character as "\t" for readability over a literal TAB.


goinstall is now history

goinstall was replaced by go get. go get is used to manage external / 3rd party libraries (e.g. to download them, update them, install them etc).

Type go help get to see command line help, or check out these pages:

Command go

About the go command (blog post)

If you want to list installed packages, you can do that with the go list command:

Listing Packages

To list packages in your workspace, go to your workspace folder and run this command:

go list ./...

./ tells to start from the current folder, ... tells to go down recursively. Of course this works in any other folders not just in your go workspace (but usually that is what you're interested in).

List All Packages

Executing

go list ...

in any folder lists all the packages, including packages of the standard library first followed by external libraries in your go workspace.

Packages and their Dependencies

If you also want to see the imported packages by each package, you can try this custom format:

go list -f "{{.ImportPath}} {{.Imports}}" ./...

-f specifies an alternate format for the list, using the syntax of package template. The struct whose fields can be referenced can be printed by the go help list command.

If you want to see all the dependencies recursively (dependencies of imported packages recursively), you can use this custom format:

go list -f "{{.ImportPath}} {{.Deps}}" ./...

But usually this is a long list and just the single imports ("{{.Imports}}") of each package is what you want.


Also see related question: What's the Go (mod) equivalent of npm-outdated?


Start Go documentation server:

godoc --http :6060

Visit http://localhost:6060/pkg

There will be list of all your packages.

When you install new ones they do not appear automatically. You need to restart godoc.

Tags:

Go