Question

List Standard Library Packages in Go

gogo-packages

Question

Is there a way to list every standard library package installed with Go? I also have a list of package import paths and need to determine which of them are standard packages rather than third-party packages.

Short Answer

You can use the Go toolchain's go list command to inspect installed packages. The special std package pattern lists standard library packages, and the .Standard metadata field lets scripts classify specific import paths reliably.

Concept

Go packages are identified by import paths, such as fmt, net/http, or github.com/example/project.

A standard library package is distributed as part of a Go installation. It is maintained with Go itself and is available without adding a dependency to your module. Examples include:

  • fmt for formatting text
  • strings for string operations
  • net/http for HTTP clients and servers
  • encoding/json for JSON encoding and decoding

The go command understands which packages belong to the standard library. Instead of guessing from a package name or folder location, ask the toolchain with go list.

The special package pattern std means: “all packages in the Go standard library.” This is more accurate and portable than hard-coding a list, because the standard library can change between Go releases.

Mental Model

Think of your Go installation as a toolbox.

  • The standard library is the set of tools already inside the toolbox when you buy it.
  • A third-party package is a tool you obtained separately and placed in your project.
  • go list std is the inventory command that prints the tools that came with the toolbox.

For an individual package, the .Standard field is like a label that says whether the tool came from the original toolbox.

Take Quiz

Syntax and Examples

List all standard library package import paths:

go list std

Example output includes paths such as:

archive/tar
bytes
encoding/json
fmt
net/http
strings

To find standard packages containing a word, pipe the result through a shell filter:

go list std | grep '^net/'

To inspect one or more package paths, ask go list to print the Standard metadata field:

go list -f '{{.ImportPath}} standard={{.Standard}}' fmt net/http github.com/gin-gonic/gin

Typical output:

fmt standard=true
net/http standard=true
github.com/gin-gonic/gin standard=false

-f supplies a Go template. For each package found, .ImportPath is its import path and .Standard is a boolean describing whether it belongs to the standard library.

To print only standard packages from a supplied set:

go list -f '{{if .Standard}}{{.ImportPath}}{{end}}' fmt net/http github.com/gin-gonic/gin

Empty lines can be removed by your shell or a later processing step.

Step by Step Execution

Consider this command:

go list -f '{{.ImportPath}}: {{.Standard}}' fmt github.com/google/uuid

What happens:

  1. The go command receives two package patterns: fmt and github.com/google/uuid.
  2. It resolves each pattern to package metadata.
  3. For fmt, Go recognizes a package distributed with the Go installation.
  4. For github.com/google/uuid, Go resolves a module dependency, if it is available to the current module.
  5. The template runs once per resolved package.
  6. .ImportPath prints the package identity, while .Standard prints true or false.

The result looks like this:

fmt: true
github.com/google/uuid: false

A package must be resolvable in the current environment for go list to inspect it. For third-party packages, run the command inside an appropriate module directory, or ensure the dependency can be resolved.

Real World Use Cases

  • Dependency audits: Separate built-in imports from external dependencies before producing a dependency report.
  • Build tooling: Ignore standard-library imports when checking for forbidden or unapproved third-party modules.
  • Code generators: Detect whether an import needs a module dependency or is already provided by Go.
  • Documentation tools: Group imports into standard and external sections.
  • Migration scripts: Scan package lists and identify dependencies that must be downloaded or vendored.
  • Learning and discovery: Browse the packages available in the installed Go version with go list std.
Take Quiz

Real Codebase Usage

In production tooling, developers usually let the Go toolchain resolve packages instead of using naming rules such as “a path without a dot is standard.” That rule is not a reliable API and can be misleading for local modules or unusual import paths.

Common patterns include:

  • Use go list -json for automation. JSON is easier and safer for programs to parse than human-formatted text.
  • Check .Standard. This is the toolchain-provided classification rather than an inferred guess.
  • Handle unresolved packages. A third-party path may fail to resolve if the command is run outside the correct module or a dependency is unavailable.
  • Use the current Go version. Running go list std during CI ensures the result matches the Go version used to build the project.

For example, tools often request only the fields they need:

go list -json -find fmt net/http

-find asks for basic package information without loading dependency information, which can be useful when you only need package identity and metadata.

Common Mistakes

Assuming a package is standard because its path has no dot

This is a tempting shortcut, but it is not a dependable way to classify a package.

// Do not use this as a standard-library test.
strings.Contains(importPath, ".")

Import path shape is not the authoritative source. Use .Standard from go list instead.

Confusing built-in language features with standard packages

len, make, new, and append are built-in functions, not packages. They do not appear in go list std and do not need importing.

items := make([]string, 0)
println(len(items))

By contrast, fmt is a standard library package and must be imported before use.

Expecting go list std to include Go commands

go list std lists standard library packages. The Go distribution's command packages use the separate cmd pattern:

go list cmd

Most application code needs std, not cmd.

Ignoring package resolution errors

This may fail when a dependency is not available to the current module:

go list github.com/example/missing-package

Run the command from the module that uses the dependency, check the import path, or use go mod download when appropriate.

Parsing default output in a program

Avoid relying on formatted text that may be inconvenient to parse. Prefer go list -json and decode JSON in your program.

Comparisons

ApproachBest useReliable standard-package classification?
go list stdList every standard library packageYes
go list -f '{{.Standard}}' packageCheck known, resolvable package pathsYes
go list -json packageBuild scripts and Go tools that need structured metadataYes
Check whether an import path contains .Quick guess onlyNo
Check whether a package is in GOROOT manuallyLow-level investigationUsually unnecessary

go list std answers “what standard packages are available?” The .Standard field answers “is this resolved package standard?” Use the latter when classifying a supplied list.

Take Quiz

Cheat Sheet

# List all standard library packages
go list std

# List Go command packages distributed with Go
go list cmd

# Classify selected packages
go list -f '{{.ImportPath}} standard={{.Standard}}' fmt net/http example.com/project

# Emit only the standard entries among selected packages
go list -f '{{if .Standard}}{{.ImportPath}}{{end}}' fmt net/http example.com/project

# Get structured data for automation
go list -json -find fmt net/http
  • std is a special go list pattern for the standard library.
  • .Standard is a boolean field in package metadata.
  • Built-in functions such as len and make are not packages.
  • Third-party packages must be resolvable in the current module context.

FAQ

How do I list all Go standard library packages?

Run:

go list std

Does go list std show packages for my installed Go version?

Yes. It uses the Go toolchain you run, so its output corresponds to that installation and version.

How can I check whether one Go package is standard?

Use the package metadata field:

go list -f '{{.Standard}}' net/http

It prints true for a standard library package.

Are fmt, strings, and net/http built-in packages?

They are standard library packages, not language built-ins. You import them with an import declaration.

Are len and make in the standard library?

No. They are built-in language functions, so they are available without importing a package.

Why does go list fail for a third-party package?

The package may not exist, may have an incorrect path, or may not be available from the module directory where you ran the command.

Should I detect standard packages by checking for a dot in the import path?

No. That is a convention-based guess, not a reliable classification method. Use go list metadata.

Take Quiz

Mini Project

Description

Build a small command-line utility that accepts package import paths and reports whether each resolved package belongs to Go's standard library. This mirrors the type of classification used by dependency-analysis tools.

Goal

Create a Go program that runs go list -json, reads package metadata, and prints each package with its standard-library status.

Requirements

Requirement 1

Take Quiz

Keep learning

Related questions

View more