by Radosław Śmigielski
You probably have your own reason(s) why you need to compile Go code for Raspberry Pi. Maybe you have own project which does not come in binary form from your Raspberry Pi operating system (Raspbian, Ubuntu). In my case, my old Raspberry Pi was too slow to compile Golang.
Raspbian GNU/Linux 12 (bookworm) on Raspberry Pi Model B Plus Rev 1.2.
Golang cross comilation is the simplest way of cross compiling code, I’ve ever seen so far ;) It was first added in Golang 1.14 and has become usable in 1.15. It allows to select your target by setting these two variables before you compile your code:
GOOS available options:
wasip1*) https://github.com/golang/go/issues/58141, https://wasi.dev
illumos**) OpenSolaris fork developed since 2010, after Oracle decided to discontinue OpenSolaris.
GOARCH available options:
(!) Not all the combinations of GOARCH and GOOS are supported. Supported combinations can be checked using this command:
go tool dist list
And this is how it looks on my Golang go1.24.10:
aix/ppc64
android/386
android/amd64
android/arm
android/arm64
darwin/amd64
darwin/arm64
dragonfly/amd64
freebsd/386
freebsd/amd64
freebsd/arm
freebsd/arm64
freebsd/riscv64
illumos/amd64
ios/amd64
ios/arm64
js/wasm
linux/386
linux/amd64
linux/arm
linux/arm64
linux/loong64
linux/mips
linux/mips64
linux/mips64le
linux/mipsle
linux/ppc64
linux/ppc64le
linux/riscv64
linux/s390x
netbsd/386
netbsd/amd64
netbsd/arm
netbsd/arm64
openbsd/386
openbsd/amd64
openbsd/arm
openbsd/arm64
openbsd/ppc64
openbsd/riscv64
plan9/386
plan9/amd64
plan9/arm
solaris/amd64
wasip1/wasm
windows/386
windows/amd64
windows/arm64
Finally. After this long into here is how you can compile Go code for different versions of Raspberry Pi:
radek@pi14:~ $ arch
armv6l
In my case this is ARM v6, in your case may be different.
GOOS=linux GOARCH=arm GOARM=6 go build run.go
$ file run
run: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), statically linked, BuildID[sha1]=51d8599ec532368d6923160997a87b4f42d0faba, with debug_info, not stripped