Cross Compiling Golang #

Mike Kusold / @mikekusold

Why? #

  • Code once, user everywhere
  • Development speed
  • Reduced infrastructure

Supported Platforms #

$GOOS		$GOARCH
darwin		386
darwin		amd64
dragonfly	386
dragonfly	amd64
freebsd		386
freebsd		amd64
freebsd		arm
linux	  	386
linux	  	amd64
linux	  	arm
netbsd		386
netbsd		amd64
netbsd		arm
openbsd		386
openbsd		amd64
plan9	  	386
plan9	  	amd64
solaris		amd64
windows		386
windows		amd64

Setting up the toolchain #

The hard way

Get the source code #

git clone https://go.googlesource.com/go
cd go/
git checkout go1.4.2
cd src/

Build for your $GOHOSTOS #

./all.bash

Build for each of your targets #

# 64 bit OSX
GOOS=darwin GOARCH=amd64 CGO_ENABLED=1 ./make.bash
# 32 Bit Windows
GOOS=windows GOARCH=386 CGO_ENABLED=1 ./make.bash
# Raspberry Pi
GOOS=linux GOARCH=arm GOARM=5 CGO_ENABLED=1 ./make.bash

Verification #

$ ls ../bin/
darwin_amd64  linux_arm  windows_386  go  gofmt

Sample Go App #

package main

import (
    "fmt"
    "os/user"
)

func main() {
    // Get Current User
    currentUser, err := user.Current()
    if err != nil {
        fmt.Println("Error: " + err.Error())
    }

    fmt.Println("My home dir is: " + currentUser.HomeDir)
}

Building the app #

go build sampleApp.go

GOOS="windows" GOARCH="386" ./go/bin/go build sampleApp.go

Demo #

The Easy Way #

gox #

github.com/mitchellh/gox

Using gox #

gox -build-toolchain
gox -osarch="darwin/amd64 linux/amd64 windows/amd64"

Thank you #

@mikekusold