Go编译文件在不同的操作系统或CPU架构上是如何工作的?

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

How Go compiled file works on different OS or CPU architectures?

问题

自从昨天开始学习Golang以来,我有一个关于编译文件的问题。

假设我编译了我的项目,它在/bin文件夹中生成了一个.exec文件。
现在我的问题是,如果我想将文件发布给公众,那么在其他操作系统和其他CPU架构(如AMD、ARM等)上是否需要重新编译?

我猜如果我在后端使用GO语言并在服务器上运行它,这应该不是个问题。然而,如果我将我的.exec文件发布到比如AWS上,那里有很多根据负载自动增加/减少的实例,会有问题吗?

编辑

对于那些正在寻找Go交叉编译工具的人来说,这是一个不错的解决方案:https://github.com/mitchellh/gox

英文:

Since I have started to learn Golang since yesterday Go编译文件在不同的操作系统或CPU架构上是如何工作的? I have a question about the compiled file.

Let's assume that I compile my project. It generates an .exec file in /bin folder.
Now my question is Since the file has been compiled on Mac with Intel based CPU, should it be compiled on other OS and other CPU architectures such as AMD, ARM, etc. if I want to publish it to public?

I guess this should not be problem if I'm using GO lang for my backend since I run it on a server. However, what happens if I publish my .exec file, let's say on AWS, with lots of instances that they are automatically increases/decreases based on load? Does it problem?

Edit:

This is nice solution for those how are looking Go cross compiling tool https://github.com/mitchellh/gox

答案1

得分: 1

第一个问题的答案是肯定的。Go的当前实现会生成本机二进制文件,因此您可能需要为Linux x86(32位)、Linux x64(64位)和Linux ARM分别编译不同的版本。您可能还需要为Mac OS X编译不同的版本。只要您所依赖的任何库在该系统上以32位形式可用,您应该能够在64位系统上运行32位可执行文件,因此您可能可以跳过制作64位可执行文件的步骤。

将来可能会有其他针对虚拟机(如JVM或.NET)编译的Go实现,这样您就不需要为不同的架构编译多个版本。您的问题更多涉及现有的Go实现,而不是语言本身。

我对AWS一无所知,但建议您将其作为一个单独的问题提问。

英文:

The answer to the first question is yes. The current implementations of Go produce a native binary, so you will probably need a different one for Linux x86 (32-bit), Linux x64 (64-bit), and Linux ARM. You will probably need a different one for Mac OS X also. You should be able to run the 32-bit executable on a 64-bit system as long as any libraries you depend on are available in 32-bit form on that system, so you might be able to skip making a 64-bit executable.

In the future, there may be other implementations of Go that compile for a virtual machine (such as JVM or .NET), in which case you wouldn't need to compile multiple versions for different architectures. Your question is more about existing Go implementations than the language itself.

I don't know anything about AWS, but I suggest you ask that as a separate question.

huangapple
  • 本文由 发表于 2015年1月15日 13:01:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/27957087.html
匿名

发表评论

匿名网友

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

确定