How do I cross compile a Go project on Linux to generate an executable for running on Windows?
To build from Linux to Windows, you need to set the environment variables GOOS
to Windows
and GOARCH
to amd64
.
On Bash or ZSH:
% GOOS=windows GOARCH=amd64 go build
If your package requires CGO
then you need to use the mingw-w64 compiler:
sudo apt-get install gcc-multilib
sudo apt-get install gcc-mingw-w64
GOOS=windows GOARCH=386 \
CGO_ENABLED=1 CXX=i686-w64-mingw32-g++ CC=i686-w64-mingw32-gcc \
go build