How do I check the size of a Golang project?

前端 未结 1 941
囚心锁ツ
囚心锁ツ 2021-01-27 19:03

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条回答
  • 2021-01-27 20:00

    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.

    0 讨论(0)
提交回复
热议问题