Cross-compiling Go code redux

Apr 28, 2017 00:00 · 97 words · 1 minute read golang compilation

Since my last post in 2015 it has become even easier to cross-compile Golang.

With any recent go compiler on my Mac OS X system, I can produce a Linux ARM binary by simply:

GOOS=linux GOARCH=arm go build

For example:

[peacock@trashcan pwgen]$ uname -a
Darwin trashcan.local 15.6.0 Darwin Kernel Version 15.6.0: Fri Feb 17 10:21:18 PST 2017; root:xnu-3248.60.11.4.1~1/RELEASE_X86_64 x86_64
[peacock@trashcan pwgen]$ GOOS=linux GOARCH=arm go build
[peacock@trashcan pwgen]$ file pwgen 
pwgen: ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, not stripped

Of course, this applies to all the various combinations of supported GOOS and GOARCH targets.