<p>I am currently refactoring a small application that imports several packages. Some of those packages are by myself, some of them by others.</p>
<p>After some refactoring I am wondering what functions of each package I am actually still using. Maybe its better to pull in or rewrite some functions so that I can discard those packages all together.</p>
<p>So is there a tool or other easy way to see what functions of which packages are actually used ?</p>
<hr/>**评论:**<br/><br/>adonovan76: <pre><p>You could try the callgraph tool:</p>
<pre><code>$ go get golang.org/x/tools/cmd/callgraph
$ ./callgraph -algo=pta -test net/http
</code></pre>
<p>It prints the entire callgraph of a program, starting from <code>main</code> or the test suite. With a little modification or postprocessing, you could print only the reachable or unreachable functions in each package.</p></pre>sethammons: <pre><p>Off the top of my head, I don't know of one. There is the AST package in the stdlib that could probably be leveraged. My first thought was to do something like:</p>
<p><code>find . -name '*.go' -exec grep github {} + | sed 's/\// /g' | sed 's/"//g' | grep -oE '[^ ]+$' | sort | uniq > packages_used</code></p>
<p>Which, I think, should get you all your packages that you use. Then do something similar and grep each file for each item in the packages_used file, grep the .go files again for matches with <code>$1.(</code> or <code>$1{</code> (where <code>$1</code> is a line from the file). That is my first thought, and I can't explore it now, or I'll be late for work.</p>
<p>If anyone finds a proper tool or makes one, that would be nice. I don't think I'll have time to try that for a few weeks yet. </p></pre>matttproud: <pre><p>I imagine you could easily and precisely accomplish this by using the <a href="https://golang.org/cmd/go/#hdr-List_packages" rel="nofollow">go list</a> command, combined with <a href="https://golang.org/s/oracle-user-manual" rel="nofollow">go oracle <callees | callers></a> on the command line.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传