dongzao4503 2019-02-17 14:04
浏览 70
已采纳

为什么Go编译器只需要包含您直接导入的软件包?

I have been using Go and have read at some places that Go compiler needs to only include the packages that you are importing directly from main.
As a result, the time it takes to resolve dependency is linear as compared to C++ where its exponential. How does that work?
If main imports package alpha which in turn imports package beta then wouldn't Go compiler need to compile the whole tree?

  • 写回答

1条回答 默认 最新

  • dongyuan8469 2019-02-17 14:28
    关注

    The Go compiler first compiles a single package into a single object file. At this stage, it only needs to resolve all the imports it finds in the package it compiles. For the object file of your package, there are only calls to the same package or direct imports, so there's no issue here. A separate linking stage links all object files together. There's a decent explanation of this in the doc of the compile package:

    Compile, typically invoked as “go tool compile,” compiles a single Go package comprising the files named on the command line. It then writes a single object file named for the basename of the first source file with a .o suffix. The object file can then be combined with other objects into a package archive or passed directly to the linker (“go tool link”). If invoked with -pack, the compiler writes an archive directly, bypassing the intermediate object file.

    The generated files contain type information about the symbols exported by the package and about types used by symbols imported by the package from other packages. It is therefore not necessary when compiling client C of package P to read the files of P's dependencies, only the compiled output of P.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭