LinuxCommandLibrary

go-list

List Go packages and their dependencies

TLDR

List packages

$ go list ./...
copy

List standard packages
$ go list std
copy

List packages in JSON format
$ go list -json time net/http
copy

List module dependencies and available updates
$ go list -m -u all
copy

SYNOPSIS

go list [flags] [patterns]

PARAMETERS

-e
    Report all errors (not just the first 10 on different lines).

-f template
    Specify an alternate template for the list, using the text/template package. See 'go help text/template' for details. The default output prints the package import path.

-json
    Print the results as a JSON array or object, instead of writing them to standard output.

-m
    List modules instead of packages.

-test
    Indicates whether to include test files.

patterns
    Package import paths to list. The patterns use the same syntax as the 'go import' command, and can include wildcards.

DESCRIPTION

The `go list` command is a powerful tool for inspecting and querying Go packages and modules. It provides detailed information about source code, dependencies, build settings, and more. It can be used to understand the structure of a Go project, verify dependencies, identify potential issues, and automate build processes. You can filter the output based on various criteria, such as package name, import path, or build tags. The command utilizes Go's understanding of modules to accurately resolve dependencies and provide information about the module graph. `go list` also helps to explore the standard library and available third-party packages. It's a cornerstone for many Go development workflows, enabling developers to gain insight into their code and its dependencies. Furthermore, it's commonly used in scripting and automation to dynamically generate build configurations, analyze code quality, or perform other tasks related to Go projects.

CAVEATS

The output format can change between Go versions, particularly when using `-f` or `-json`. Be mindful of this when writing scripts that rely on `go list` output.

TEMPLATES

Using the `-f` flag with a text template, you can extract specific fields from the package information. For example, `go list -f '{{.Name}} {{.Imports}}'` will print the package name and its import list.

MODULE MODE

When using the `-m` flag, `go list` operates in module mode. It provides information about Go modules, including their versions, dependencies, and locations. `go list -m all` can be used to list all modules in the current module graph.

WILDCARDS

The patterns argument supports wildcards. The '...' wildcard matches any sequence of characters, including multiple path elements, and it can be used to get recursively the package names inside of a directory.
For example:
go list ./... will list all packages in the current directory and its subdirectories.

HISTORY

The `go list` command has been part of the Go toolchain since its early days. It has evolved over time to incorporate module support and new output formats, making it increasingly powerful for managing and understanding Go projects. Initially focused on package listing, its scope expanded with the introduction of modules in Go 1.11 to encompass module dependency analysis.

SEE ALSO

go get(1), go mod(1), go build(1)

Copied to clipboard