Skip to content

Instantly share code, notes, and snippets.

@cespare
Created March 20, 2014 19:03
Show Gist options
  • Select an option

  • Save cespare/9671365 to your computer and use it in GitHub Desktop.

Select an option

Save cespare/9671365 to your computer and use it in GitHub Desktop.

Revisions

  1. cespare revised this gist Mar 20, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion results.txt
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    $ go run deps.go
    $ go run unused.go
    container/ring
    debug/gosym
    debug/pe
  2. cespare created this gist Mar 20, 2014.
    22 changes: 22 additions & 0 deletions results.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    $ go run deps.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
    46 changes: 46 additions & 0 deletions unused.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    // 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)
    }
    }
    }