如何在客户端机器上运行Go文件?

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

How to run a Go file on client machine?

问题

我有一个Go文件,我想把它给客户使用。客户应该能够在终端中使用一个命令来执行它。他们不需要在他们的系统中安装Go语言。

如何在客户机上运行这个二进制文件?请帮助我。

英文:

I have a Go file and I would like to give it to the customers. The customers should be able to execute using a command in terminal. They will not install Go lang in their system.

How to do run this binary file in clients machine? Please help me.

答案1

得分: 4

Go是一种编译语言。你可以使用Go工具将你的Go应用程序编译成可执行二进制文件。一旦你有了可执行二进制文件,你可以直接运行它,不再需要或使用Go安装。由于Go是静态链接的,所有的依赖项都将被编译/链接到可执行文件中。

具体来说:

  • 使用go install编译你的应用程序,并将可执行二进制文件“安装”到$GOPATH/bin文件夹中
  • 或者使用go build编译你的应用程序,并在当前工作目录中获取可执行二进制文件

阅读https://stackoverflow.com/questions/30612611/what-does-go-build-build了解更多细节。

你只需要将可执行二进制文件传递给你的客户。如果你的应用程序使用静态文件,当然你也需要传递它们,最好打包成一个压缩文件。

请注意,可执行二进制文件针对特定的平台。你需要与你的客户商讨他打算在哪个平台上运行你的应用程序,并将应用程序编译为该特定平台。

目标操作系统和平台可以使用GOOSGOARCH环境变量进行设置。例如,如果你的客户想在Windows amd64上运行你的应用程序,你可以这样生成一个Windows amd64可执行文件:

GOOS=windows GOARCH=amd64 go build

有关有效的GOOSGOARCH值的列表,请参阅https://stackoverflow.com/questions/37302211/how-can-i-build-for-linux-32-bit-with-go1-6-2/37302937#37302937。

英文:

Go is a compiled language. You can use the Go tools to compile your Go app to an executable binary. Once you have the executable binary, you can run it as-is, the Go installation is not needed / used anymore. Since Go is statically linked, all the dependencies will be compiled / linked into the executable.

More specifically:

  • use go install to compile your app and "install" the executable binary to your $GOPATH/bin folder
  • or use go build to compile your app and get the executable binary in the current working directory

Read https://stackoverflow.com/questions/30612611/what-does-go-build-build for more details.

All you need is to pass on the executable binary to your customer. If your app uses static files, of course you have to pass them too, preferably packed into one compressed file.

Note that an executable binary targets a specific platform. You have to consult your customer about what platform he intends to run your app on, and compile your app to that specific platform.

The target OS and platform can be set using the GOOS and GOARCH environment variables. For example, if your customer wants to run your app on Windows amd64, you can produce a Windows amd64 executable like this:

GOOS=windows GOARCH=amd64 go build

For a list of valid GOOS and GOARCH values, see https://stackoverflow.com/questions/37302211/how-can-i-build-for-linux-32-bit-with-go1-6-2/37302937#37302937.

huangapple
  • 本文由 发表于 2017年1月11日 18:03:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/41587816.html
匿名

发表评论

匿名网友

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

确定