As part of my studies with Go, yesterday tried to compile a simple project to windows from my ubuntu linux, and I've got some small tips for that.
1 - To Crosscompile you just need to change 2 enviroment variables, GOARCH (the architecture of the processor) and GOOS (the operational system), you could do this by using export:
But this makes you need to revert it later.
For a 1 line approach you can pass the parameters before run build command.
GOOS=windows GOARCH=386 go build
2 - If you are using some C library with cgo, you gonna need to enable the CGO_ENABLED flag, and have the structure to compile the C library properly (mingw for windows and things like that). Ex:
GOOS=windows GOARCH=386 CGO_ENABLED=1 CC=i686-w64-mingw32-gcc CXX=i686-w64-mingw32-g++ go build -ldflags "-H windowsgui" <= this didn't work.
In some cases you can use xgo ( https://github.com/karalabe/xgo ):
xgo --targets=windows/* . would compile to windows.
It was relatively easy to crosscompile, lets check later with larger projects.
If you have some case to tell or some suggestion, please comment.
One critical skill to have as a more senior developer is to be able to view potential problems as a series of patterns that have already been solved somewhere else. Let's take a bit of a dive into some of them!