(1)首先进入go/src 源码所在目录,执行如下命令创建目标平台所需的包和工具文件。
$ cd /usr/local/go/src
$ CGO_ENABLED=0 GOOS=linux GOARCH=amd64 ./make.bash
如果是 Windows 则修改 GOOS 即可。
$ CGO_ENABLED=0 GOOS=windows GOARCH=amd64 ./make.bash
以上假定是64位架构,32位,修改GOARCH为386
mips架构的cpu
//下载go-mips32源
git clone https://github.com/gomini/go-mips32.git
cd go-mips32/src
//配置GO编译参数
export GOOS=linux
export GOARCH=mips32
//执行编译
./make.bash
(2) 现在可以编译 Linux 和 Windows 平台所需的执行文件了。
$ CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build
$ CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build
$ CGO_ENABLED=0 GOOS=linux GOARCH=mips32 go build