How To Set Up a Simple Go Web Server on a Raspberry Pi
If you have a Raspberry Pi, one of the first projects you may find yourself doing is setting it up as a web server. While there are a number of options available for web servers such as Apache, NodeJS and NginX, I thought I’d take a different route today and try it with Go. If you aren’t familiar with Go, you can go (I promise that’s the last bad pun) to the web site and check it out. The documentation and tutorials are very thorough which is a huge help for us new Go programmers. In short, Go is an open source programming language created by Google in 2007 which has quickly becoming a preferred language among technologists due to its ease of use and clean syntax. As you’ll see, this will be fairly well illustrated here.
INSTALLING GO ON THE RASPBERRY PI
The Go web site offers fairly comprehensive instructions for Linux on the installation process. However, as you can see, there is no tarball for an ARM processor. To get around this without having to compile on your own, you can visit Dave Cheney’s web site for a list of unofficial tarballs. The only thing you’ll need at this point is the url of the tarball that Dave provides. Please make sure to read his instructions carefully because he has a number of versions available and only one is intended for the Raspberry Pi.
STEP 1: DOWNLOAD GO AND INSTALL IT
$ cd /usr/local $ sudo wget http://dave.cheney.net/paste/go.1.3.linux-arm~multiarch-armv6-1.tar.gz $ tar -C /usr/local -xzf go.1.3.linux-arm~multiarch-armv6–1.tar.gz
STEP 2: UPDATE YOUR PATH VARIABLES
Open your .profile file and add the following three lines to the bottom. Please note that if you’ve added a .bash_profile file or a .bash_login this file will not be read:
export PATH=$PATH:/usr/local/go/bin export GOPATH=$HOME/go export PATH=$PATH:$GOPATH/bin
Note that above we set the path of the Go programming language as well as the path to our workspace. You can install Go into a directory other then /usr/local/ if you wish. Those instructions are included on the Go web site for Linux installs. Once you’re done, you’ll need to either reboot your Raspberry Pi or simply just source the file (sourcing the file is much quicker):
$ source ~/.profile
Once this is done, check to make sure Go was actually installed properly by typing ‘go’ at the command line. You should see something like this:
$ go Go is a tool for managing Go source code. Usage: go command [arguments] The commands are: build compile packages and dependencies clean remove object files env print Go environment information fix run go tool fix on packages fmt run gofmt on package sources get download and install packages and dependencies install compile and install packages and dependencies list list packages run compile and run Go program test test packages tool run specified go tool version print Go version vet run go tool vet on packages Use “go help [command]” for more information about a command. Additional help topics: c calling between Go and C filetype file types gopath GOPATH environment variable importpath import path syntax packages description of package lists testflag description of testing flags testfunc description of testing functions Use “go help [topic]” for more information about that topic.
STEP 3: SET UP THE PROPER WORKSPACE DIRECTORY STRUCTURE
The Go web site goes into significant detail on the proper directory structure for a Go project which I highly recommend reading. However, if you’re looking for the “Cliff Notes” version, you can find it here. Basically, there are three directories that are generally present, only two of which are going to be used today and only one we’re going to explicitly create. The proper structure ideally should contain a src, bin and pkg directory. However, we are only going to explicitly create the src directory as the bin directory will be created on build.
$ mkdir -p $GOPATH/src/github.com/[YOUR_NAME]/server $ sudo pico $GOPATH/src/github.com/[YOUR_NAME]/server/server.go
As you can see above, we created the proper path and associated directories for our web server and opened Pico to edit the server.go application. I chose Pico, but any terminal text editor will work just fine (my apologies to the Vim loyalists; my skills just aren’t up to par yet). Now add the following to the server.go file, save it and close it:
package main import “net/http” func main() { http.ListenAndServe(“:1337", http.FileServer(http.Dir(“/var/www/.”))) }
Yep, it really IS that simple. We import the net/http package, ask it to listen on port 1337 and return requests from the /var/www/ directory (we’re getting to that step next).
STEP 4: CREATE THE WEB DIRECTORY AND ASSOCIATED FILE(S)
So far so good; now we just need to create the web directory, add in a default index.html and BOOM, we’re done (almost). Let’s go ahead and create that directory and add in the index.html:
$ mkdir /var/www $ sudo chown pi www $ sudo pico /var/www/index.html
A couple of notes about the lines above. We created a directory under /var called “www”. You can put your web files just about anywhere; this is just my personal preference. Also note that we chowned (or “changed owner”) on the directory “www”. this is so that the base user can make changes to the contents of that directory without having to be root. Once you have index.html open, add in a basic HTML page (I’ve included one below to save time):
<!DOCTYPE html> <html> <head> <title>Raspberry Pi Go Web Server</title> </head> <body> <h1>Welcome to my Go web server!!</h1> </body> </html>
Ok, almost done. One final step remains between you and Go greatness!!
STEP 5: TURN THAT SUCKER ON!
If you did everything correctly, you should be able to type the name of your application at the command line and pull it up in a browser (please note your IP address will differ from the one shown):
$ server
Now if you open a web browser and type in the IP address of your Raspberry Pi, followed by the port number 1337, you should see something like this:


Congratulations! You have officially built a Go web server on a Raspberry Pi.
THANKING THOSE WHO MADE THIS POSSIBLE
As much as I would love to claim that I thought of all of this on my own, that’s simply not the case. As with many things that are on the web, this was a collaborated effort.
I’d like to take a moment to thank Dave Cheney both for having a thorough write up on the unofficial Go tarballs and for making them publicly available. If you haven’t checked out his web site, please do stop by. It’s got lots of tips, tricks and genius tucked away between the pages.
I’d also like to thank Matt Ho who helped me figure out the VERY simple code for the Go web server (trust me, the version I was originally going to “go to press” with would NOT have been this easy or well written).
As always, please feel free to send me comments, criticisms, corrections and refactors. If they make the process easier, I’ll happily add them in.