Compiling Golang Applications for Raspberry Pi

As you know golang applications can be run multiple architectures (amd64, arm, etc)

Today we will write a small golang application and compile it for ARM architecture so that it can run on a RaspberryPi

Golang Build Environment

From my RaspberryPi, I will use docker to drop into a alpine golang environment:

$ docker run -it arm32v7/golang:alpine sh
/go #

Get the dependencies that we will require to build our compile our golang binary:

$ apk add --no-cache gcc libc-dev

Hello World Golang App

Just a simple golang application for demonstrative purposes:

package main

import "fmt"

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

Running the app:

$ go run app.go
Hello, World!  

Compiling the Binary

Since we are on a ARM based architecture, we can simply just run go build app.go which will compile our binary.

But if you were compiling it from a Linux AMD64 architecture type, you need to cross compile it for it to be running on the target desire type.

For demonstration, I will show how to compile it for ARM based architecture:

$ GOOS=linux GOARCH=arm GOARM=5 go build app.go

When we look in our working directory, we will find the binary:

$ find . -type f -name app
./app

Running the Application

Running our hello world application:

$ ./app
Hello, World!