How to List all Standard Packages in Go

Created
Modified

Using packages

go install

Use the following command to download the repository to the local file system.

install
go get golang.org/x/tools/go/packages
go install golang.org/x/tools/go/[email protected]

Note that since Go 1.17 installing packages with go get is deprecated:

packages Usage

Package packages loads Go packages for inspection and analysis.

For example,

package main

import (
  "fmt"

  "golang.org/x/tools/go/packages"
)

func main() {
  pkgs, err := packages.Load(nil, "std")
  if err != nil {
    panic(err)
  }

  fmt.Println(pkgs)
}
[archive/tar bufio bytes...]

Using exec.Command Function

Use the go list std command to list the standard packages. The special import path std expands to all packages in the standard Go library.

See the following example:

package main

import (
  "fmt"
  "os/exec"
  "strings"
)

func main() {
  cmd := exec.Command("go", "list", "std")
  o, err := cmd.Output()
  if err != nil {
    panic(err)
  }
  pkgs := strings.Fields(string(o))
  fmt.Println(pkgs)
}
[archive/tar bufio bytes...]

Related Tags

#read# #file# #string#