How to list installed packages in Golang? [SOLVED]


GO, GOLANG Solutions

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
archive/tar
archive/zip
bufio
bytes
compress/bzip2
compress/flate
compress/gzip
compress/lzw
...

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
archive/tar
archive/zip
bufio
bytes
compress/bzip2
compress/flate
compress/gzip
compress/lzw
...

 

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
github.com/davecgh/go-spew v1.1.1
github.com/pmezard/go-difflib v1.0.0
github.com/sirupsen/logrus v1.9.0
github.com/stretchr/objx v0.1.0
github.com/stretchr/testify v1.7.0
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405
gopkg.in/yaml.v3 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 (
    "fmt"
    "os/exec"
)

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

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

Output:

# go run main.go 
tar
zip
bufio
bytes
bzip2
flate

 

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
/root/go

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

# find /root/go -name *.go
/root/go/pkg/mod/golang.org/x/sys@v0.2.0/windows/svc/example/main.go
/root/go/pkg/mod/golang.org/x/sys@v0.0.0-20220715151400-c0bba94af5f8/windows/svc/example/main.go
/root/go/pkg/mod/golang.org/x/tools@v0.0.0-20191109212701-97ad0ed33101/cmd/bundle/main.go
/root/go/pkg/mod/golang.org/x/tools@v0.0.0-20191109212701-97ad0ed33101/cmd/callgraph/main.go
/root/go/pkg/mod/golang.org/x/tools@v0.0.0-20191109212701-97ad0ed33101/cmd/compilebench/main.go
/root/go/pkg/mod/golang.org/x/tools@v0.0.0-20191109212701-97ad0ed33101/cmd/cover/testdata/main.go
/root/go/pkg/mod/golang.org/x/tools@v0.0.0-20191109212701-97ad0ed33101/cmd/fiximports/main.go
...

 

Summary

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.

 

References

How to list installed go packages - Stack Overflow

 

Deepak Prasad

Deepak Prasad

Deepak Prasad is the founder of GoLinuxCloud, bringing over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, Networking, and Security. His extensive experience spans development, DevOps, networking, and security, ensuring robust and efficient solutions for diverse projects.

Certifications and Credentials:

  • Certified Kubernetes Application Developer (CKAD)
  • Go Developer Certification
  • Linux Foundation Certified System Administrator (LFCS)
  • Certified Ethical Hacker (CEH)
  • Python Institute PCAP (Certified Associate in Python Programming)
You can connect with him on his LinkedIn profile and join his Facebook and LinkedIn page.

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 send mail to admin@golinuxcloud.com

Thank You for your support!!

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

Leave a Comment