Google doesn’t yet offer any pre-built binary packages for the Raspberry Pi. While there is a golang package for Raspbian, at the time of writing, it is for an older version of Go. Therefore the best way to install Go on a Raspberry Pi is to build it from source. This may sound a bit daunting but it is actually quite easy. The first step is to install Mercurial, the revision control system used by Google to manage the Go source code. The principle command line interface to the Mercurial system is hg and with it, you can download the source code to Go. The following command will download the source necessary to build Go into /usr/local/go: It will probably take about 10 minutes depending on your Internet connection. Once downloaded, you can start the build process. Change directory to the downloaded source code using cd /usr/local/go/src and then start the build process: There will be lots of output during the build, mainly informational messages. The build will take somewhere between 90 minutes and two hours. Once completed, the final output will look like this:
The build process has prompted us to add the directory containing the Go binary to the PATH. To do this you need to edit .profile which the Bash processes whenever you create a new shell. Change directory back to your home directory by using cd without any parameters and then edit .profile using the nano editor: At the end of the file, add the following line: Press CTRL + X to leave the nano editor and type Y to confirm that the file should be saved. Press ENTER to keep the current filename (i.e. .profile). You will need to exit the terminal and restart another (if you are using a desktop environment) or log out and log back in again (if you are using the command line). To check that Go is functioning correctly create a file called hello.go with the following lines: And then run it using: The result will be a single line of text which reads: Hello Make Tech Easier!
Things to try
Go is a very powerful programming language and it is designed for writing programs which work with multiple tasks at once. Known as concurrency, you can write a program to execute lots of tasks simultaneously but also allow these separate tasks to communicate and interact. Look at our Writing Your First Concurrent Go Program article and try the code examples and see the power of a concurrent programming language on a multitasking operating system, all running on your Raspberry Pi!