问题描述

是否有一种简单的方法来检查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$GOPATHgo$HOME/go
$GOPATH/pkg$GOPATHgo$HOME/go
gorilla
gorilla
$ go get -u github.com/gorilla/mux
$ go get -u github.com/gorilla/securecookie
$ go get -u github.com/gorilla/sessions
darwin_amd64
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() {}
KB
KB
$ du -k *

1028    empty
5812    http
5832    mux
net/httpmuxnet/httpmuxhttp20KB284KB
net/httpmuxnet/httpmuxhttp20KB284KB

结论: 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屋!