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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?