Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用 go_dep_search 分析项目里面的包依赖 #23

ma6174 opened this issue May 19, 2019 · 1 comment


Copy link

commented May 19, 2019



之前有种比较麻烦但是也相对有效的方法。我们知道Go的编译是有缓存的,如果代码有变更的话就会触发包重新编译,通过 go build -v可以把重新编译的包的名字输出出来,当然也包含最终的服务程序。实际操作的时候需要避免ide编译影响。

后来研究了一下,实际有更好的办法来实现。go list命令有个-json的参数,会自动分析各个包的依赖关系,以json的格式输出出来,我们以官方库里面的encoding/base64包为例:


# go list -json encoding/base64
	"Dir": "/usr/local/go/src/encoding/base64",
	"ImportPath": "encoding/base64",
	"Name": "base64",
	"Doc": "Package base64 implements base64 encoding as specified by RFC 4648.",
	"Target": "/usr/local/go/pkg/linux_amd64/encoding/base64.a",
	"Root": "/usr/local/go",
	"Match": [
	"Goroot": true,
	"Standard": true,
	"GoFiles": [
	"Imports": [
	"Deps": [
	"TestGoFiles": [
	"TestImports": [
	"XTestGoFiles": [
	"XTestImports": [


  • ImportPath:在执行包引用时的名字 encoding/base64
  • Name:包的名字 base64
  • Imports:这个包直接引用的包:"encoding/binary", "io", "strconv"
  • Deps:这个包所有递归依赖的包

有了上面的Deps信息之后,开头提到的依赖分析问题就比较简单了。只要在Deps里面有我们修改的包就可以了,如果要找是哪个服务的话,需要看Namemain的包。另外go list -json ./... 就可以分析当前项目里面的所有包。

go list -json ./... 输出的信息还是挺多的,为了方便分析,写了个小工具 go_dep_search (


/usr/local/go/src# go list -json ./... | go_dep_search -main net/http encoding/json
Name	->	ImportPath	->	dep_package
main	->	cmd/compile	->	encoding/json
main	->	cmd/cover	->	encoding/json
main	->	cmd/dist	->	encoding/json
main	->	cmd/go	->	net/http
main	->	cmd/go	->	encoding/json
main	->	cmd/link	->	encoding/json
main	->	cmd/pprof	->	net/http
main	->	cmd/pprof	->	encoding/json
main	->	cmd/test2json	->	encoding/json
main	->	cmd/trace	->	net/http
main	->	cmd/trace	->	encoding/json
main	->	cmd/vet	->	encoding/json



This comment has been minimized.

Copy link
Owner Author

commented Jun 2, 2019

新版的 go_dep_search 支持分析包调用链、支持找出未被使用的包。

Repository owner deleted a comment from kirainl Jul 9, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
1 participant
You can’t perform that action at this time.