Go programming on a Raspberry Pi
Way to Go
![© Lead Image © Nelli Valova, 123RF.com © Lead Image © Nelli Valova, 123RF.com](/var/linux_magazin/storage/images/issues/2019/225/go-on-the-rasp-pi/po-26683-123rf-nelli_valova_123rf_com-steampunk_lights_and_gears_resized.png/750873-1-eng-US/PO-26683-123RF-Nelli_Valova_123RF_com-Steampunk_lights_and_gears_resized.png1_medium.png)
© Lead Image © Nelli Valova, 123RF.com
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.
![Learn More](https://www.linux-magazine.com/var/linux_magazin/storage/images/media/linux-magazine-eng-us/images/misc/learn-more/834592-1-eng-US/Learn-More_medium.png)
News
-
NVIDIA Released Driver for Upcoming NVIDIA 560 GPU for Linux
Not only has NVIDIA released the driver for its upcoming CPU series, it's the first release that defaults to using open-source GPU kernel modules.
-
OpenMandriva Lx 24.07 Released
If you’re into rolling release Linux distributions, OpenMandriva ROME has a new snapshot with a new kernel.
-
Kernel 6.10 Available for General Usage
Linus Torvalds has released the 6.10 kernel and it includes significant performance increases for Intel Core hybrid systems and more.
-
TUXEDO Computers Releases InfinityBook Pro 14 Gen9 Laptop
Sporting either AMD or Intel CPUs, the TUXEDO InfinityBook Pro 14 is an extremely compact, lightweight, sturdy powerhouse.
-
Google Extends Support for Linux Kernels Used for Android
Because the LTS Linux kernel releases are so important to Android, Google has decided to extend the support period beyond that offered by the kernel development team.
-
Linux Mint 22 Stable Delayed
If you're anxious about getting your hands on the stable release of Linux Mint 22, it looks as if you're going to have to wait a bit longer.
-
Nitrux 3.5.1 Available for Install
The latest version of the immutable, systemd-free distribution includes an updated kernel and NVIDIA driver.
-
Debian 12.6 Released with Plenty of Bug Fixes and Updates
The sixth update to Debian "Bookworm" is all about security mitigations and making adjustments for some "serious problems."
-
Canonical Offers 12-Year LTS for Open Source Docker Images
Canonical is expanding its LTS offering to reach beyond the DEB packages with a new distro-less Docker image.
-
Plasma Desktop 6.1 Released with Several Enhancements
If you're a fan of Plasma Desktop, you should be excited about this new point release.