In a recent discussion on Go’s Google Plus Community, some questions came up about cross compiling. In a default installation of Go, cross compiling is not enabled. It only builds to toolchains for your current operating system and architecture. Since all of my computers are amd64, but some of the applications I build need to run on i386, I thought I’d share how I go about doing it.

I use Arch linux, so I have provided a package in AUR for doing this. It makes setup extremely simply. You can install the package by doing the following:

$wget https://aur.archlinux.org/packages/go/go-cross-compilers-linux/PKGBUILD$ makepkg

$# For amd64$ sudo pacman -U go-cross-compilers-linux-1.0.3-1-x86_64.pkg.tar.xz

$# For i386$ sudo pacman -U go-cross-compilers-linux-1.0.3-1-i686.pkg.tar.xz


Now that the other toolchain is in place, we can make a sample program to test it out:

package main

import (
"fmt"
)

func main() {
fmt.Println("Hello world!")
}


If you were building it for your own architecture, something as simple as the following would work:

$go build main.go$ ./main
Hello world!


Building for the other architecture isn’t much more difficult. Go will cross compile other operating systems and architectures based on the environmental variables GOOS and GOOARCH. In our case, we are interested in changing GOARCH:

$GOARCH=386 go build main.go  Now you can copy main over your i386 machine and run it! If you were cross compiling to amd64, you’d simply replace 386 with amd64. One last interesting tidbit is that if you have a version of gcc that will cross compile, then Go applications that link to C libraries can also be used (despite what many people have suggested). In Arch, you can install a cross compiling version of gcc like this: $ sudo pacman -S gcc-multilib


In this example, I’m going to test a sqlite3 library, so let’s install the 32-bit libraries:

$sudo pacman -S lib32-sqlite  Now, we let’s build a small app with the example from go-sqlite3: $ go get github.com/mattn/go-sqlite3
$mkdir testsql$ cd testsql
$wget https://raw.github.com/mattn/go-sqlite3/master/example/main.go$ # Test the program locally to make sure it works.
$go run main.go$ # Build for i386
GOARCH=386 go build

$# Verify it's linked against the right libraries (lib32).$ ldd testsql
linux-gate.so.1 (0xf77ba000)
libsqlite3.so.0 => /usr/lib32/libsqlite3.so.0 (0xf76e2000)