Golang development for Raspberry Pi 3. Getting started

Eduard Iskandarov
4 min readApr 2, 2019

Overview

This guide will focus on initial steps of application development with go programming language.

We will use Raspberry Pi 3 and up to date version of Golang(1.12 at the moment of writing).

Raspberry Pi 3 comes with Cortex-A53 (ARMv8) 64-bit SoC, which allows us building applications for ARM 64-bit processors.

There’re several components in home lab author has:

  1. Raspberry Pi 3 Model B+
  2. Raspberry Pi PoE HAT(this hat is not required. Moreover it’s inactive due to the author hadn’t received compatible networking device yet. I will demonstrate it’s feature next time)
  3. Mikrotik RB760iGS(this device is used as the author’s home router and is suitable for connecting Raspberry Pi to a local network)
Figure 1. Raspberry Pi 3 Model B+ is connected to the router

Raspberry Pi is powered by micro USB supply and is connected to the router’s port 2 via Ethernet. Micro SD card with the operating system is inserter. The device is up and running.

Picking the right operating system

The current version of official operating systems Raspbian and NOOBS do no support 64-bit architecture and work on armhf(arm hard float).

Luckily, Ubuntu released Ubuntu Server for ARM:

Ubuntu 18.04.2 LTS includes support for the very latest ARM-based server systems powered by certified 64-bit processors.

Prebuilt images for Raspberry Pi 3 are available at https://wiki.ubuntu.com/ARM/RaspberryPi.

Upon downloading, ensure arm64 release was picked.

Flashing operating system image to SD card

There are many tutorials on the topic. The right tool depends on the operating system you use on a desktop computer.

For now, I’ll mention what worked for me.

The simplest way of burning an OS image to the SD card is balenaEtcher. There’re versions for Linux, macOS, and Windows.

Following simple steps should be enough for most cases:

  1. open Etcher
  2. select image (file name should look like ubuntu-18.04.2-preinstalled-server-arm64+raspi3.img.xz)
  3. plug in the card to your desktop computer
  4. select drive
  5. flash!

Then insert micro SD card to the Raspberry Pi SD card slot, and we’re ready to go.

Compiling binary executable for Raspberry Pi on the desktop computer

I will not cover the topic of setting up a Golang environment, such as Go distribution installation, go P, and integrated development environment setup.

Instead, we’ll build a hello world application and run it on the microcomputer.

Create somewhere file hello.go with the following content:

Open terminal, change current working directory to file’s directory.

Then build it with the go tool:

$ go build

At this point, we build an executable for current arch. You can run it with command ./hello. If everything works as expected — hello, world message will be printed in the terminal.

Finally, let’s build an executable for ARM 64-bit processor.

$ GOOS=linux GOARCH=arm64 go build

Since Golang natively supports cross-compilation, all we need is specify compilation parameters with GOOS and GOARCH environment variables. Please refer to the documentation for more information and possible values.

For this case, we instruct the compiler that target operating system should be Linux and running on ARM 64-bit processor.

Figure 2. Build and run Golang hello world program

Delivering executable to Raspberry Pi

This is an easy step if Raspberry’s IP address is known.

To figure our IP address, the author used the router’s web UI:

Figure 3. Router’s web UI with DHCP Server Leases page

After filtering our all connected devices — only one is left. Its hostname is ubuntu, what perfectly matches to the operating system, we installed on the device.

Thus, the IP address of Raspberry Pi is 192.168.99.244.

Let’s open an SSH connection to the device first and ensure it works as expected.

Caution, on the first login — ubuntu will ask to authenticate host and reset the password.

Figure 4. The first SSH connection to Ubuntu on Raspberry Pi

Eventually, we can deliver an executable to the device and test it.

The simplest tool to transfer a file over the local network to a Linux machine is an SCP(secure copy) utility. For this exercise, we’ll use the executable file that was prepared on one of the previous steps.

$ scp hello ubuntu@192.168.99.244:
$ ssh ubuntu@192.168.99.244
$ ./hello
> hello, world

An interactive demonstration is available below:

Figure 5. Cross-compilation and run on Raspberry Pi

Summary

In this mini-tutorial, we have built a hello world program written in Golang.

Using Golang cross compiling feature we could conveniently prepare executable for Raspberry Pi 3 on a local machine and transferred to the device over the local network.

Raspberry Pi successfully ran executable and it opens freedom for further experiments and development.

It worth to say that due to native support of ARM 64-bit processes by Ubuntu we can fully utilize all processors features.

In the next topics, the author will open more details of Golang programming for Raspberry Pi, such as working with GPIO, camera serial interface and display serial interface ports.

--

--

Eduard Iskandarov

Entrepreneurial engineer. Full-stack development, DevOps, Security. Building software, teams and startups.