How to list installed packages in Golang? [SOLVED]

Written By - admin

In this tutorial we will show different methods to show where are go packages installed or to list all installed packages in golang.


Example-1: Using go list command

In golang you can use the go list command to list the packages that are part of the standard library. The go list command takes a list of patterns as arguments, and it returns the packages that match those patterns. By default, go list will list all packages in the standard library.

Here is an example to list all the packages from standard library:

# go list std

We can also use go list command with wildcards, to list only the packages that match a specific pattern.


Example-2: List all exportable packages

To list only exportable packages we can use "-e" flag with go list command:

# go list -e std


Example-3: List all installed packages

We can use the -m flag with go list to list only the packages that are installed in the main module.

# go list -m all
goexamples v1.1.1 v1.0.0 v1.9.0 v0.1.0 v1.7.0 v0.0.0-20220715151400-c0bba94af5f8 v0.0.0-20161208181325-20d25e280405 v3.0.0-20200313102051-9f266ea9e77c

To print in JSON format, we can use -m -json as below:

# go list -m -json all
        "Path": "goexamples",
        "Main": true,
        "Dir": "/root/goexamples",
        "GoMod": "/root/goexamples/go.mod",
        "GoVersion": "1.17"


Example-2: Formatting the go list command output

We can use -f flag with go list to format the output. The flag takes a format string as an argument, which specifies how the output should be formatted. Here's an example of how you might use the -f flag to list only the package names in the standard library :

package main

import (

func main() {
    cmd := exec.Command("go", "list", "-f", "{{.Name}}", "std")
    out, err := cmd.Output()
    if err != nil {

This code will execute the go list -f {{.Name}} std command and print the package names in the standard library.

The {{.Name}} format string is a Go template that tells the go list command to print the Name field of the package. Other fields that can be accessed using this format are :

  • {{.ImportPath}}: the import path of the package
  • {{.Doc}}: the package documentation
  • {{.Dir}} : the directory containing the package


# go run main.go 


Example-3: Using go doc command

We can also utilise go doc command which takes a package name as an argument and it returns information about the package. You can use the -all flag to see all the exported types, functions and variables. If you just want to list the package names, you can pipe the output of the go doc command to the grep command and search for package names.

# go doc -all std | grep func
func (f Format) String() string
func FileInfoHeader(fi fs.FileInfo, link string) (*Header, error)
func (h *Header) FileInfo() fs.FileInfo
func NewReader(r io.Reader) *Reader
func (tr *Reader) Next() (*Header, error)
func (tr *Reader) Read(b []byte) (int, error)
func NewWriter(w io.Writer) *Writer
func (tw *Writer) Close() error
func (tw *Writer) Flush() error
func (tw *Writer) Write(b []byte) (int, error)
func (tw *Writer) WriteHeader(hdr *Header) error


Example-4: Using find command to search all installed packages

We can also use find command to find all the go files in your GOPATH:

#go env GOPATH

Next use find command to search for all .go files which will give you a list of installed packages:

# find /root/go -name *.go



In this tutorial we covered different possible methods to search for all the installed and available packages in our system. The most used command is go list which can give you most of the information, but alternatively we also have some more commands and methods such as looking inside GOROOT and GOPATH for installed packages. We can further add different flags with go list to isolate and filter our search.



How to list installed go packages - Stack Overflow


Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

2 thoughts on “How to list installed packages in Golang? [SOLVED]”

Leave a Comment