powershell - Windows 上的 golang : How to build for Linux?

标签 powershell go environment-variables cross-compiling

<分区>

如何在 Windows 上为 Linux 编译一个 go 文件/项目?

在 Linux 上应该可以通过一行代码实现:

GOOS=windows go build -o filename.exe codefile.go

我通过 Powershell 尝试了类似的操作,但在我的 Linux 服务器(Ubuntu 16.04 amd64)上只出现了一个错误:

-bash: ./gotest: cannot execute binary file: Exec format error".

我尝试使用用户 Env-vars 为 GOOS = linuxGOARCH = amd64 并在 Powershell 中通过 set GOOS = linux,但我对 Powershell 了解不够 - go build 命令运行时没有错误并生成文件。

任何人都可以使用 go-1.10 通过 Powershell(或 cmd)(通过 VS 代码)解释在 Windows (10 1709 x64) 上的一般操作方法吗?

最佳答案

如果你的机器上安装了 git bash 并执行以下命令,请使用它

env GOOS=linux go build -o filename

您可以通过以下方式为整个命令 session 设置执行操作系统:

set GOOS=linux
go build -o filename

它在相同的 Windows 10 版本上为我工作,并在 Ubuntu 16.04 LTS 上顺利运行

关于powershell - Windows 上的 golang : How to build for Linux?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49449190/

相关文章:

environment-variables - 如何将私有(private)信息添加到 OpenShift 环境变量?

powershell - 向外部程序发送多个命令

powershell - 如何在 Powershell 中发出经过身份验证的 Web 请求?

powershell if-else 不遵循任何一个分支

regex - regexp.Compile 和 regexp.CompilePOSIX 有什么区别?

linux - Puppet:设置环境变量和启动应用程序 - 变量未定义

c - 简单的 _putenv 示例不起作用

powershell - 如何为 Azure 应用服务(如 Visual Studio 2015 UI)生成 pubxml 文件?

mongodb - 如何通过golang中的mgo在mongo中插入math/big.Int

http - 从 http 响应主体读取 golang