Go programming on a Raspberry Pi
Simple Go Web App
The web_static.go
web application in Listing 2 shows the static web page in Listing 3. The web app uses the net/http library to define a default handler (line 9), that calls the static page (line 10). The app then listens and serves up data on port 8081 (line 14).
Listing 2
web_static.go
Listing 3
web_static.html
To compile and test web_static.go
, enter:
$ go build web_static.go $ ./web_static Default Web Page
When you use a web browser to call the Rasp Pi address at port 8081 (Figure 2), you should see the message This is a static test page from the Go application every time the page is called.
GPIO Web App
The final step is to pull everything together so that a web page can pass parameters to a Go app that shells out to the gpio
command-line utility. To begin, you create new web page (go_buttons.html
) with two buttons (Listing 4). HTML anchor tags pass the /on
and /off
parameters to the web app.
Listing 4
web_buttons.html
A CACHE-CONTROL
meta tag set to NO-STORE
ensures that the web page always refreshes. If you don't include this meta tag, the web page might only update once.
The Go GPIO web app go_buttons.go
(Listing 5) now includes two additional http.HandleFunc
handler functions: one for the /on
parameter (line 30) and one for the /off
parameter (line 37). These handler functions pass the required pin state to a function called gpio
.
Listing 5
go_buttons.go
The newly created gpio
function acts very much like the earlier writepin7.go
code (Listing 1), in that it shells out twice to the gpio
command-line utility: the first time to write a value and the second time to read the value back. Web page requests, GPIO pin results, and errors are shown in the terminal.
The syntax to compile and run the go_buttons.go
code with some sample web requests is:
$ go build go_buttons.go $ ./go_buttons Default Web Page Web Page sent : ON GPIO Pin 4 value : 1 Default Web Page Web Page sent : OFF GPIO Pin 4 value : 0
Figure 3 shows the web output and the test setup.
Summary
Go offers a robust platform for web applications. From the basic web page example in this article, I created more advanced Rasp Pi projects, such as the rover shown in Figure 4 that required writing to multiple GPIO pins.
For a polished application, I would rather use a native Go library for GPIO calls, but for prototyping and troubleshooting, I found the gpio
command-line utility was very useful.
The next step for my Rasp Pi projects will be to add JavaScript and Ajax to show dynamic values.
Infos
- Go programming language: https://golang.org/
« Previous 1 2
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
-
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.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.
-
DebConf24 to be Held in South Korea
Busan will be the location of the latest DebConf running July 28 through August 4