Skip to content

Instantly share code, notes, and snippets.

@cespare
Created March 20, 2014 19:03
Show Gist options
  • Save cespare/9671365 to your computer and use it in GitHub Desktop.
Save cespare/9671365 to your computer and use it in GitHub Desktop.
Print out stdlib packages not used within the stdlib.
$ go run unused.go
container/ring
debug/gosym
debug/pe
encoding/ascii85
encoding/csv
expvar
go/build
hash/crc64
hash/fnv
index/suffixarray
log/syslog
net/http/cookiejar
net/http/fcgi
net/http/pprof
net/mail
net/rpc/jsonrpc
net/smtp
os/user
runtime/cgo
runtime/race
text/scanner
// Print a list of stdlib packages not used in the stdlib.
package main
import (
"bytes"
"fmt"
"go/build"
"log"
"os/exec"
"strings"
)
func main() {
// Use 'go list std' to get a list of stdlib packages.
// Maybe there's a better way.
out, err := exec.Command("go", "list", "std").Output()
if err != nil {
log.Fatal(err)
}
var pkgs []string
for _, name := range bytes.Fields(out) {
pkg := string(name)
if !strings.HasPrefix(pkg, "cmd/") {
pkgs = append(pkgs, string(name))
}
}
used := make(map[string]bool)
for _, name := range pkgs {
pkg, err := build.Import(name, "", 0)
if err != nil {
log.Fatal(err)
}
imports := append(pkg.Imports, pkg.TestImports...)
imports = append(imports, pkg.XTestImports...)
for _, imp := range imports {
used[imp] = true
}
}
for _, name := range pkgs {
if !used[name] {
fmt.Println(name)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment