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/pkgdirectory (if$GOPATHis not exportedgodefaults to$HOME/go).So to check the size of some of the
gorillahttp pkgs. Install them first:$ go get -u github.com/gorilla/mux $ go get -u github.com/gorilla/securecookie $ go get -u github.com/gorilla/sessionsThe 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.aEDIT:
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 muxWhat does this tell us? The core go pkg
net/httpadds significant size to our executable. Themuxpkg is not large by itself, but it has an import dependency onnet/httppkg - hence the significant file size for it too. Yet the delta betweenmuxandhttpis only20KB, whereas the listed file size of the mux.a library is284KB. 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.
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报