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:
fmtfor formatting textstringsfor string operationsnet/httpfor HTTP clients and serversencoding/jsonfor 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 stdis 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.
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:
- The
gocommand receives two package patterns:fmtandgithub.com/google/uuid. - It resolves each pattern to package metadata.
- For
fmt, Go recognizes a package distributed with the Go installation. - For
github.com/google/uuid, Go resolves a module dependency, if it is available to the current module. - The template runs once per resolved package.
.ImportPathprints the package identity, while.Standardprintstrueorfalse.
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.
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 -jsonfor 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 stdduring 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
| Approach | Best use | Reliable standard-package classification? |
|---|---|---|
go list std | List every standard library package | Yes |
go list -f '{{.Standard}}' package | Check known, resolvable package paths | Yes |
go list -json package | Build scripts and Go tools that need structured metadata | Yes |
Check whether an import path contains . | Quick guess only | No |
Check whether a package is in GOROOT manually | Low-level investigation | Usually 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.
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
stdis a specialgo listpattern for the standard library..Standardis a boolean field in package metadata.- Built-in functions such as
lenandmakeare 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.
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
Keep learning
Related questions
Are Go Maps Passed by Value or Reference?
Learn Go map parameter and return semantics: maps are passed by value, while copies share underlying data. Know when map pointers are useful.
Authentication for Go REST APIs: Sessions, Tokens, and Middleware
Learn practical authentication patterns for Go REST APIs, including password hashing, server sessions, bearer tokens, middleware, and security rules.
Automatic Build Versioning in Go: Embed Incrementing Build Numbers
Learn how to add automatic build versioning in Go using linker flags, build metadata, CI counters, and Git-based version values.