Go Cross Compilation from Windows to Linux/Ubuntu

 I have GO 1.7 installed on my Windows 10. I created test program and it works perfectly in Windows. Next step is to try to run it on my docker virtual machine with Ubuntu.

 

Answer:

That other question is a bit old (from 2013).

​GOARCH​​GOOS​​go build​
​main​
set GOARCH=amd64
set GOOS=linux
go build


You may change the name of the result binary like this:

go build -o "myapp"


​main​
GOOS=windows GOARCH=amd64 go build


GOOSGOARCHgo build -v YOURPACKAGE

Notes

​go install​​GOPATH​
​go build​
​go build​​go install​​go build​​go install​​go install​​.a​​pkg/​
wget https://storage.googleapis.com/golang/go1.4.2.linux-amd64.tar.gz

设置环境变量

 GOROOT_BOOTSTRAP=/home/aikangs/app/go1.4.2
export GOROOT_BOOTSTRAP
export PATH=${PATH}:${GOROOT_BOOTSTRAP}

最后进入GOROOT/src目录运行你需要编译的平台的代码


# 如果你想在Windows 32位系统下运行
~$CGO_ENABLED=0 GOOS=windows GOARCH=386 ./make.bash

# 如果你想在Windows 64位系统下运行
~$CGO_ENABLED=0 GOOS=windows GOARCH=amd64 ./make.

# 如果你想在OS X 32位系统下运行
~$CGO_ENABLED=0 GOOS=darwin GOARCH=386 ./make.bash

# 如果你想在OS X 64位系统下运行
~$CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 ./make.bash

# 如果你想在Linux 32位系统下运行
~$CGO_ENABLED=0 GOOS=linux GOARCH=386 ./make.bash

# 如果你想在Linux 64位系统下运行
~$CGO_ENABLED=0 GOOS=linux GOARCH=amd64 ./make.bash

最后编译时,进入你的项目目录 运行

# 如果你想在Windows 32位系统下运行
➜ ~$CGO_ENABLED=0 GOOS=windows GOARCH=386 go build main.go

# 如果你想在Windows 64位系统下运行
➜ ~$CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build main.go

# 如果你想在OS X 32位系统下运行
➜ ~$CGO_ENABLED=0 GOOS=darwin GOARCH=386 go build main.go

# 如果你想在OS X 64位系统下运行
➜ ~$CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build main.go

# 如果你想在Linux 32位系统下运行
➜ ~$CGO_ENABLED=0 GOOS=linux GOARCH=386 go build main.go

# 如果你想在Linux 64位系统下运行
➜ ~$CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build main.go