Google's Go programming language looks like something interesting to explore and one of the aims of the Raspberry Pi is to enable people to learn programming. However if you are using the new Raspbian distribution you may have noticed that the golang package does not work due to problems with the way it is compiled. The package installs without issue but if you attempt to run Go it crashes in a quite ugly way. Fortunately thanks to the power of the Debian package management system used on Raspbian it is relatively straightforward to grab the source of the package, change it and rebuild it so it does work. This is preferable to rebuilding it from the source code on the Go website as it means that the work done to integrate it with the operating system will remain intact.
Getting what you need
Before going any further make sure all of the packages on your system are up to date and make sure that the build-essential package is installed:
sudo apt-get install build-essential
Now you need to make sure that you can get the sources of the packages. We need to tell apt, the package manager where these can be found. To do this create a new file: /etc/apt/sources.list.d/src.list enter this line:
deb-src http://mirrordirector.raspbian.org/raspbian/ wheezy main
Update apt so it loads in the information needed:
sudo apt-get update
As rebuilding the package involves downloading lots of files it is worth having a directory, so create it and move into it like this:
mkdir golang
cd golang
Grab the source of the package with this command:
apt-get source golang
This source package actually builds three binary packages, but we only have to make a couple of details. Now we need to make sure that we have everything needed to build the package:
sudo apt-get build-dep golang
As you can see here the package management system is making life a bit easier for us!
Changing the rules
The fixes needed to make the package work on the Raspberry Pi are actually quite minor. We only need to change two files and then we can rebuild. Inside our golang working directory you will see that a few files have been downloaded as a result of getting the source code. The code has also been extracted to a directory called golang-1.0.2. The files we need to change are not actually the source code files but the files describing how the package gets built. These are found in the golang-1.0.2 directory, change to this and open up the file rules in your text editor and look for these two lines:
else ifeq ($(DEB_HOST_ARCH), armhf)
GOARM := 6
The rules file controls how the package gets build and this is the line that causes the problem for the Raspberry Pi. Unfortunately this entry in the rules file cause the Go packages to be compiled for a later version of the ARM architecture than is present on the Raspberry Pi (the armhf (ARM CPUs with hardware floating point support) release of Debian is actually targeted these later architecture chips. So comment out the offending line and add a new entry to target the right architecture:
else ifeq ($(DEB_HOST_ARCH), armhf)
# Has to be 5 on the Raspberry Pi
# GOARM := 6
GOARM := 5
The last change to make is to increase the version number and to record some details of the changes we have made. This is done in the changelog file. We do not need to add too many details here but care is needed with the formatting. If you open it up in a text editor you should see the entries for the last few changes. Add an entry to the top that looks a bit like this:
golang (2:1.0.2-2) unstable; urgency=low
* Changed the GOARM setting to compile for Raspberry Pi
-- Your Name
Enter your name and email address and update the date and time. This has to be in RFC2822 format, you can enter this at the command line to get the current value:
date -R
Make sure to leave two spaces between the email address and date and a blank line between your entry and the entry below. If you do not get this right you will see an error in the next step.
It is time to build the package! Go the directory above the debian directory:
cd ..
As we are just going to be building the package for our own use we are not going to bother digitally signing it or anything like that. Start the build process with:
dpkg-buildpackage -us -uc
Installation
The build too about thirty-file minutes on my Raspberry Pi (which is overclocked to 850MHz) so now could be a good time to grab a meal! After the build process is finished you need to change to your golang working directory (which should be the directory above where you are now) you will see several binary packages (the files ending in *.deb) have been built. These need to be installed in a certain order to work and as we are installing local packages we need to do these installs with a slightly different command:
sudo dpkg -i golang-src_1.0.2-2_armhf.deb
sudo dpkg -i golang-go_1.0.2-2_armhf.deb
sudo dpkg -i golang-doc_1.0.2-2_all.deb
sudo dpkg -i golang_1.0.2-2_all.deb
Some of the other debs that have been built will add syntax highlighting to your favourite text editors for Go so you might want to investigate them. You should now be able to start programming in Go on your Raspberry Pi! Remember though to set up your GOPATH as described in the Go documentation. It is even possible to host and run the Go tour on the Pi!
Photo: Raspberry by VeniVidiVoided