DISCLAIMER: Word on the street some of the material contained in this post has been depricated by later versions of go...
Gox is useful tool I discovered that enables simple cross compilation in a Go project.
Write once, run…where?
go build by default will produce a binary based on your local operating system and architecture unless you specify otherwise. This seems logical and straight forward.
We can specify a particular os/arch using something like:
env GOOS=darwin GOARCH=arm go build -v <myAmazingProject>
Cool! Go programs are compiled as static binaries without dynamic dependencies, thus they can be distributed easily without needing to have Go installed on the host machine.
Likely the application will be Dockerized, the image pushed to a registry like Dockerhub or AWS Docker (er….ECR? I have no idea really). Assuming you’ll be using some flavor of Linux, you have nothing to worry about. Just make sure to compile a Linux binary and voila!
However, when you have to start thinking about supporting a myriad of different operating systems and architectures, things become a little more complex.
A combination of various Makefile targets and some shell scripting could solve the problem we’re trying to address here, but those could quickly become cumbersome, unwieldy, and difficult to maintain.
Gox is a simple tool that will solve all of your problems (if all of your problems are related to cross compilation in Go).
Using the following command, we can produce multiple binaries for each platform, concurrently.
gox -osarch="linux/amd64 darwin/amd64"
gox without any parameters and it will produce a binary for every operating system and architecture under the sun.
$ gox Number of parallel builds: 7 --> linux/arm: SignumCreator --> netbsd/arm: SignumCreator --> freebsd/amd64: SignumCreator --> windows/amd64: SignumCreator --> darwin/386: SignumCreator --> openbsd/386: SignumCreator --> freebsd/386: SignumCreator --> netbsd/386: SignumCreator --> freebsd/arm: SignumCreator --> openbsd/amd64: SignumCreator --> windows/386: SignumCreator --> netbsd/amd64: SignumCreator --> linux/386: SignumCreator --> darwin/amd64: SignumCreator --> linux/amd64: SignumCreator
Boom. Rather than spending time perfecting your build scripts and mastering the art of writing a good Makefile (both are cool, don’t get me wrong) you can spend more time focusing on your application code itself. Fin