如何检查Golang项目的大小? [英] How do I check the size of a Golang project?

查看:118
本文介绍了如何检查Golang项目的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法来检查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.

推荐答案

您可以通过查看$GOPATH/pkg目录来查看库二进制文件的大小(如果未导出$GOPATH,则go默认为$HOME/go ).

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).

因此,要检查某些gorilla http pkgs的大小.首先安装它们:

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

我的64位MacOS(darwin_amd64)上的KB二进制大小:

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

库(包)的大小是一回事,但是在链接阶段之后,可执行文件中占用的空间可能有很大的不同.这是因为程序包具有自己的依赖关系,并且附带了额外的行李,但行李可能会与您导入的其他程序包共享.

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() {}

所有3个程序在功能上都是相同的-执行零用户代码-但它们的依赖性不同.结果在KB:

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

这告诉我们什么?核心go pkg net/http给我们的可执行文件增加了很多大小. mux pkg本身并不大,但是对net/http pkg具有导入依赖关系-因此它的文件大小也很大.但是muxhttp之间的增量仅为20KB,而列出的mux.a库文件大小为284KB.因此,我们不能简单地添加库的pkg大小来确定其实际占用空间.

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.

结论: go链接器会在构建过程中从各个库中剥离很多行李,但是为了真正了解导入某些程序包的 weight 额外的多少,必须查看所有pkg的子依赖项.

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.

这篇关于如何检查Golang项目的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆