为什么我在src目录中不能删除源文件,但还需要pkg目录?

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

go: Why do I need the pkg directory if I cannot delete the source files in src?

问题

我写了一个函数(不是主函数),然后使用go install命令。这个命令在我的_pkg-directory_中生成了一个路径和一个包。我通过在主函数中使用该函数进行测试,生成了.exe文件,一切都正常工作。之后,我想看看我是否正确理解了_go_中的包的概念,于是我删除了_src-directory_中函数的源文件,并删除了_main .exe_文件。我没有删除_pkg-directory_中的包文件。然后我尝试再次go installmain .exe,但是它不起作用:"找不到包"。显然我误解了整个概念,因为我以为我可以在没有_src_中的源文件的情况下使用_pkg_中的包。如果我的结论是正确的,那么我为什么还需要_pkg_目录呢?

在/bin目录中是主函数"hello"的二进制代码。这个主函数还包含了"stringutil"包的"reverse"函数。

通过生成"hello.exe",Go也将"stringutil"包生成到了pkg中。

我的问题是:我不应该能够删除src中的"reverse.go"并仍然能够使用相同的函数吗?因为它已经被放入pkg中了。

这只是AST的工作方式吗?现在他们已经用go重写了编译器,它在解析"imports"时会检查GOPATH/src/**/.go,然后当链接器构建最终的二进制文件时,它会去检查pkg。所以编译器在尝试将ast传递给汇编器时首先报错,因为源代码树不完整。

非常感谢!

英文:

I wrote a function (not! main) and prompted go install. This command generated a path and a package in my pkg-directory. I tested the function by using it in a main function, generated the .exe and everything worked just fine.
After that, I wanted to see if I understood the concept of packages in go correctly and deleted the source file of the function in the src-directory and deleted the main .exe. I did not remove the package file in my pkg-directory. Then I tried to go install the main .exe again, but it didn't work: "package can not be found". I obviously missunderstood the whole concept because I thought I could use the packages in pkg without the source files in src. If my conclusion was correct, why do I need the "pkg" directory at all?


For more explanation take a look at this picture please:

In /bin is the binary code of the main function "hello". This main function also contains the function "reverse" of the "stringutil" package.

By generating the "hello.exe", Go also generates the package "stringutil" into pkg.

My question is: Should I not be able to delete "reverse.go" in src and still be able to use the same function because it was already put into pkg?


Is it just the way the AST works now that they've rewritten the compiler in go? It checks for GOPATH/src/**/.go when it parses the "imports", then when the linker goes to build the final binary, it'll go check pkg. So the compiler errors out first when trying to feed the ast to the assembler because of the incomplete source tree.

Thank you very much!!!

答案1

得分: 2

pkg目录通常用作缓存目录,但也可以使用没有源代码的软件包,这是通过一个名为“binary only packages”的功能实现的。据我所知,这个功能是从Go 1.7版本开始实现的。然而,这种方法有一个注意事项:用于构建软件包的编译器版本和用于“使用”软件包生成新库/可执行文件的编译器必须匹配。此外,文件必须与要构建的操作系统/架构配对。如果你想进行交叉编译,你需要将你的软件包分发给每一对要构建的操作系统/架构。

这个项目中有一个演示了上述功能的示例。

希望我的解释足够详细 为什么我在src目录中不能删除源文件,但还需要pkg目录?

英文:

It is true that the pkg dir is usually used as a cache directory, but also It is possible to use packages without having the source code available, with a feature named binary only packages AFAIK It was implemented since Go 1.7.
However, there's a caveat for this approach: Versions of the compiler used to build the package and the compiler to 'use' the package to generate a new library/executable must match. Also, the files must match the pair of os/architecture to build against. If you want cross-compiling, you'll need to distribute your package to every pair of os/architecture you'll want to build.

This project has a demo for the aforementioned feature.

I hope my explanation was detailed enough 为什么我在src目录中不能删除源文件,但还需要pkg目录?

答案2

得分: 0

pkg目录只是用于编译的本地缓存,它不是你可以依赖的东西,也不能替代.go文件,它不是一个静态库目录,而是一个临时构建产品目录。它可以加快编译速度,所以你不需要pkg目录,但如果它为空,编译可能会变慢,而且它是编译器使用的,不是你的。

正如你已经发现的那样,你确实需要src目录。

要将静态库链接到你的项目中,你可以复制那个.a文件并使用ldflags进行链接,但这样你就失去了诸如交叉编译和拥有整个应用程序源代码等好处,所以与C语言不同,人们通常不这样做。

英文:

The pkg dir is just a local cache for compiling, it's not something you can depend upon and it doesn't replace .go files, it's not a static lib dir but a temp build products dir. It speeds up compilation, so you don't need the pkg dir, but compiles might be slower if it is empty, and it is for the compiler's use, not yours.

As you've discovered, you do need the src dir.

To link a static library to your project, you could copy out that .a file and use ldflags to link but then you lose all the nice things like cross-compiling and having the entire source for your app, so unlike c for example people don't typically do that.

huangapple
  • 本文由 发表于 2017年8月27日 23:59:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/45906808.html
匿名

发表评论

匿名网友

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

确定