Go 在 Windows 编译成 Linux 系统的二进制可执行文件

Go · 2023-07-07

默认执行 go build 执行的是当前平台环境。
若想要编译好的包运行到其他环境Linux,

set CGO_ENABLED=0
set GOARCH=amd64
go env -w GOARCH=amd64
go env -w GOOS=windows
set GOOS=linux
go build

**参数说明
CGO_ENABLED : CGO 表示golang中的工具,CGO_ENABLED 表示CGO禁用,交叉编译中不能使用CGO的
GOOS : 目标平台
mac 对应 darwin
linux 对应 linux
windows 对应 windows
GOARCH :目标平台的体系架构【386,amd64,arm】, 目前市面上的个人电脑一般都是amd64架构的
386 也称 x86 对应 32位操作系统
amd64 也称 x64 对应 64位操作系统
arm 这种架构一般用于嵌入式开发。 比如 Android , IOS , Win mobile , TIZEN 等**

golang 在 Mac , Linux , Windows 下交叉编译
Go 在 Windows/Linux/Mac交叉编译成指定系统的二进制可执行文件

编译
Theme Jasmine by Kent Liao