doubipeng1336 2019-03-13 00:04
浏览 46
已采纳

如何检查Golang项目的大小?

Is there an easy way to check the size of a Golang project? It's not an executable, it's a package that I'm importing in my own project.

  • 写回答

1条回答 默认 最新

  • duanqian3953 2019-03-13 00:55
    关注

    You can see how big the library binaries are by looking in the $GOPATH/pkg directory (if $GOPATH is not exported go defaults to $HOME/go).

    So to check the size of some of the gorilla http pkgs. Install them first:

    $ go get -u github.com/gorilla/mux
    $ go get -u github.com/gorilla/securecookie
    $ go get -u github.com/gorilla/sessions
    

    The KB binary sizes on my 64-bit MacOS (darwin_amd64):

    $ cd $GOPATH/pkg/darwin_amd64/github.com/gorilla/
    $ du -k *
    
    284 mux.a
    128 securecookie.a
    128 sessions.a
    

    EDIT:

    Library (package) size is one thing, but how much space that takes up in your executable after the link stage can vary wildly. This is because packages have their own dependencies and with that comes extra baggage, but that baggage may be shared by other packages you import.

    An example demonstrates this best:

    empty.go:

    package main
    
    func main() {}
    

    http.go:

    package main
    
    import "net/http"
    
    var _ = http.Serve
    
    func main() {}
    

    mux.go:

    package main
    
    import "github.com/gorilla/mux"
    
    var _ = mux.NewRouter
    
    func main() {}
    

    All 3 programs are functionally identical - executing zero user code - but their dependencies differ. The resulting binary sizes in KB:

    $ du -k *
    
    1028    empty
    5812    http
    5832    mux
    

    What does this tell us? The core go pkg net/http adds significant size to our executable. The mux pkg is not large by itself, but it has an import dependency on net/http pkg - hence the significant file size for it too. Yet the delta between mux and http is only 20KB, whereas the listed file size of the mux.a library is 284KB. So we can't simply add the library pkg sizes to determine their true footprint.

    Conclusion: The go linker will strip out a lot of baggage from individual libraries during the build process, but in order to get a true sense of how much extra weight importing certain packages, one has to look at all of the pkg's sub-dependencies as well.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch