Cross-compiling Go

1 min read · go, programming

I saw a thread on cross-compilation in golang-nuts group recently and decided to replicate information here so I won’t forget and (maybe/hopefully) it’ll make it easier to discover for others how to cross-compile go applications.

I’m on OS X and my Go source code resides in ~/var/go. In this case what I do is:

> cd ~/var/go/src
> CGO_ENABLED=0 GOOS=linux GOARCH=amd64 ./make.bash
> CGO_ENABLED=0 GOOS=windows GOARCH=amd64 ./make.bash

And afterwards in different directory:

> CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o gr-linux64 goreplace
> CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o gr-win64 goreplace

This has some limitations - you can’t cross-compile applications, which are using CGo. In particular this means applications importing package net. Which is a shame, but still, cross-compilation has never been easier. :)

UPD. Also see comment on reddit for more.

UPD. In case you miss something, there is a question on StackOverflow with different wording, which may help.

If you like what you read — subscribe to my Twitter, I always post links to new posts there. Or, in case you're an old school person longing for an ancient technology, put a link to my RSS feed in your feed reader (it's actually Atom feed, but who cares).

Other recent posts

ngrok for the wicked, or expose your ports comfortably
PostgreSQL collation
History snapshotting in TwinSpark.js
Code streaming: hundred ounces of nuances
Useful shell prompt