Get started with the Go programming language on your Raspberry Pi
A step-by-step guide for installing and setting up Go on Raspberry Pi, including installation instructions, environment setup, and basic usage. …
Updated October 26, 2023
A step-by-step guide for installing and setting up Go on Raspberry Pi, including installation instructions, environment setup, and basic usage.
Introduction
Go (Golang) is an open source programming language developed by Google that offers simplicity, speed, and efficiency. It’s a powerful alternative to languages like Python or C++ for developing IoT applications, system tools, and more. In this article, we will guide you through the process of installing Go on your Raspberry Pi, setting up your development environment, and writing a basic “Hello, World!” program in Go.
Prerequisites
To follow along with this tutorial, you should have:
- A Raspberry Pi device (any model will do)
- An SD card with the latest version of Raspbian installed (Stretch or later recommended)
- Access to an internet-connected device to download files and update your system
- Basic knowledge of command line usage
Installing Go on Raspberry Pi
First, we need to check which version of Raspbian is running on our Raspberry Pi:
cat /etc/os-release
Make sure the version is Stretch or later (version 9 or higher). If not, you’ll need to update your system first.
Update your package lists and upgrade any existing packages:
sudo apt-get update && sudo apt-get upgrade
Download the latest version of Go for ARM architecture (Raspberry Pi uses ARM processors):
wget https://dl.google.com/go/go1.15.7.linux-armv6l.tar.gz
Extract the downloaded archive to
/usr/local
:sudo tar -C /usr/local -xzf go1.15.7.linux-armv6l.tar.gz
Add Go to your system’s PATH:
echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.bashrc && source ~/.bashrc
Verify that Go is installed correctly by running:
go version
This should output the installed Go version, for example
go version go1.15.7 linux/arm
.
Setting up your environment
To start writing Go programs on your Raspberry Pi, you’ll need to set up a workspace directory. A workspace is a folder where all your Go code and dependencies are stored. Follow these steps:
- Create a new directory for your workspace:
mkdir ~/go-workspace
- Set the
GOPATH
environment variable to point to your workspace directory:echo 'export GOPATH=$HOME/go-workspace' >> ~/.bashrc && source ~/.bashrc
- Create a subdirectory for your first project (you can name it anything):
mkdir -p $GOPATH/src/github.com/{username}/hello cd $GOPATH/src/github.com/{username}/hello
Replace
{username}
with your GitHub username or any other unique identifier.
Writing a “Hello, World!” program in Go
Now that you have everything set up, let’s write a basic “Hello, World!” program in Go:
- Create a new file named
main.go
in your project directory and open it with your favorite text editor:nano main.go
- Add the following code to the file:
package main import "fmt" func main() { fmt.Println("Hello, World!") }
- Save and exit the file (in nano, press
CTRL+X
, thenY
andEnter
). - Build your program:
go build
- Run your program:
./hello
This should output
Hello, World!
. Congratulations, you’ve just written a Go program on your Raspberry Pi!
Conclusion
In this article, we covered how to install and set up the Go programming language on your Raspberry Pi. We also wrote a basic “Hello, World!” program in Go to verify that everything is working correctly. From here, you can continue learning more about the Go language by reading the official documentation or exploring online resources like tutorials and example projects. Happy coding!