英文:
Does the go compiler compile the package that's never used in main
问题
如果我有一个包含三个包(A、B、C)的 Go 模块。在 main.go 和其所有导入的代码中,只使用了 A、B 两个包。我的问题是,通过 go build
生成的二进制文件中是否包含来自包 C 的任何代码?
英文:
If I have a go module that has three package like A, B, C. In the main.go and all of its import, only A, B packages are ever used. My question is, does the binary produced by go build
has any code from package C?
答案1
得分: 1
二进制构建将仅包括从主函数引用的所有符号的传递闭包。这将仅包括来自导入包的函数和数据,以及使用的类型的所有方法。因此,如果您有一个从未使用过的包中的函数,它将不会出现在二进制文件中。但是,如果您使用了带有未使用方法的数据类型,这些方法将出现在二进制文件中。
英文:
The binary build will only include the transitive closure of all symbols referenced from main. This will include only those functions and data from the imported packages, and all the methods of types that are used. So if you have a function in a package that is never used, that will not be in the binary. However if you use a data type with unused methods, those methods will be in the binary.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论