Golang: How to cross compile on Linux for Windows

shagul picture shagul · Jan 10, 2017 · Viewed 13.9k times · Source

How do I cross compile a Go project on Linux to generate an executable for running on Windows?

Answer

Michael Miller picture Michael Miller · Jan 10, 2017

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