传递依赖是否内置于默认(静态链接)的最终 Go 程序二进制文件中?

huangapple go评论82阅读模式
英文:

Are transitive dependencies built into a default (statically linked) final Go program binary?

问题

在Go编程语言中,假设使用默认的“静态链接”构建,我想知道以下情况:假设我的go.mod文件中有2个模块,但go.sum文件中有50个模块(假设每个go.mod中的2个模块都有25个传递依赖),这50个模块是否都会被构建到我的程序输出的二进制文件中?也就是说,go build命令是否会获取这些传递依赖并将它们全部编译,并将它们静态链接到输出的二进制文件中?如果是这样,它是针对整个模块还是只针对程序实际引用的部分进行的呢?

对于vendored依赖,它的行为是否相同?

英文:

In the Go programming language, assuming a default "statically linked" build, I'm wondering if, for example in the case I have 2 modules in go.mod but 50 in go.sum, (assume 25 transitive for each of the 2 in my go.mod), are all 50 of the modules listed in go.sum actually going to be built into my program's output binary? That is, does go build fetch those transitive dependencies and compile them all, statically linking them into the output binary? If so, Does it do so for the entire modules or only the portions actually referenced by my program?

Does it behave in the same manner for vendored dependencies?

答案1

得分: 1

Go只会构建和链接实际被你的程序部分引用的包(包括传递引用)。

正如你所指出的,go.sum文件可能比你的项目实际需要构建的包集合要大得多(在你的系统上,使用所选配置)。go.sum(和go.mod)文件中将包含对仅在某些配置下才需要的包的引用,这是其中一个原因。

英文:

Go will only build and link the packages actually referenced by the parts of your program being built (including transitive references).

As you noted, the go.sum file may be larger--possibly much larger--than the set of packages your project actually requires in order to build (on your system, with the selected configuration). Among other reasons, the go.sum (and go.mod) file will contain references to packages which are only needed under certain configurations.

huangapple
  • 本文由 发表于 2021年8月1日 07:41:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/68606521.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定