Go programming on a Raspberry Pi
Way to Go
We show you how to create a Go web app that controls Raspberry Pi I/O.
Go, or GoLang [1], is used widely for web development. Created by Google in 2009, Go is one of the fastest growing programming languages. One of its advantages is speed: Go compiles directly to native executable files in Windows, macOS, iOS, and Linux operating systems. However, Go can also be run in interpreted mode, like Python, while debugging. In this article, I create a Go web app that talks to the Raspberry Pi hardware.
Getting Started
To install Go on a Rasp Pi, enter:
$ sudo apt-get install golang
To test the install, you can check the Go version number:
$ go version go version go1.7.4 linux/arm
A simple "Hello World" Go program named hello.go
,
package main func main() { println("Hello World"); }
can be run in interpreted mode with:
$ go run hello.go Hello World
Running in interpreted mode is handy when you are doing a lot of debugging, but the compiled Go program runs considerably faster. To compile and run the hello.go example, enter:
$ go build hello.go $ ./hello Hello World
Now that the Rasp Pi has Go installed and working, the next step is to set up the Rasp Pi hardware.
Raspberry Pi Setup
Raspberry Pi connects to sensors and hardware through the general purpose input/output (GPIO) pins. For my test setup, I connected an LED to physical pin 7 (Figure 1) and a 330-ohm resistor to prevent damage to my Rasp Pi.
By default, all the Rasp Pi GPIO pins are set as inputs, but you can configure the mode of pin 7 to output
then set the output (light the LED), and read back the pin status – all with the gpio
command-line utility:
$ gpio mode 7 output # set pin 7 as an output $ gpio write 7 1 # 1=set, 0=reset $ gpio read 7 1
The gpio write 7 1
and gpio write 7 0
commands let you test the circuit and toggle the LED on and off.
Go and GPIO
A number of Go libraries and options can read and write to GPIO pins. For this example, I shell out (i.e., spawn programs through an intermediate shell ) to the gpio
command-line utility. For prototyping, I like this approach because I can test and verify the GPIO commands manually before writing any code.
Listing 1, writepin7.go
, uses the os/exec library to create the shell command variable testCmd
(line 10) that defines the gpio
command and its parameters. The shell command is executed by testCmd.Output()
(line 11). The gpio write
command has no output, but the gpio read
(defined in line 18) returns the state of the GPIO pin (line 24).
Listing 1
writepin7.go
To run the code in interpreted mode, enter:
$ go run writepin7.go GPIO Pin 7 value : 1
If the circuit is wired correctly, the light should be on.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.