goで現在のパッケージが依存するパッケージを調べたい
goで依存するパッケージを調べたい。意外とgo list
コマンドが便利に使えた。
# そのパッケージ自体 $ go list -f "{{if not .Standard}}{{.ImportPath}}{{end}}" -deps # cwd以下の全てのパッケージでの依存 $ go list -f "{{if not .Standard}}{{.ImportPath}}{{end}}" -deps ./...
(ただし自分自身も含まれる)
例えばgoimportsの例。
$ cd $GOPATH/src/golang.org/x/tools/cmd/goimports $ go list -f "{{if not .Standard}}{{.ImportPath}}{{end}}" -deps ./... golang.org/x/tools/go/ast/astutil golang.org/x/tools/go/internal/gcimporter golang.org/x/tools/go/gcexportdata golang.org/x/tools/go/internal/cgo golang.org/x/tools/internal/fastwalk golang.org/x/tools/internal/gopathwalk golang.org/x/tools/internal/semver golang.org/x/tools/go/packages golang.org/x/tools/imports golang.org/x/tools/cmd/goimports
詳細
go help <topic>
が意外と便利。-f
でgoのtemplateパッケージで利用できる記法を使って出力を変更できる(とは言えこの種のオプションは意外と使いかたを覚えられなかったりどのような変数が使えるのか分からなかったりする)。go listに限って言うとgo help list
を覗くと何の変数が使えるか分かるので便利。.Standard
で標準ライブラリかどうかを調べられるので除外できる。
あと、 -deps
で依存をかき集めてくれるので便利。
$ go help list # 出力を抜粋 ... The -f flag specifies an alternate format for the list, using the syntax of package template. The default output is equivalent to -f '{{.ImportPath}}'. The struct being passed to the template is: type Package struct { Dir string // directory containing package sources ImportPath string // import path of package in dir ... Standard bool // is this package part of the standard Go library? ... The -deps flag causes list to iterate over not just the named packages but also all their dependencies. It visits them in a depth-first post-order traversal, so that a package is listed only after all its dependencies. Packages not explicitly listed on the command line will have the DepOnly field set to true.
後々はgo.modとか見れば済むようになるんだろうか?