Installing Go on Raspberry Pi 2 and creating a simple file server

Installing Go on Raspberry Pi 2 and creating a simple file server

Installing

This guide will describe how to get going with Go (golang) on Raspberry Pi 2, with the Raspbian Jessie distribution. I have not tried this on the older pi, nor any other compatible dist, but the Go libs are compiled to most dists and architectures, so in practice it should work fine.

Getting up and running with Go on Raspbian is ridiculously easy, to the point of this short tutorial almost being completely redundant 🙂
First you want to install the golang libraries on your Raspberry. Personally, I connect over the SSH terminal since I’m running headless.

sudo apt-get install golang

To test that Go is installed properly, try typing ‘go’ and a message on how to use Go should appear.

go

Running

Once you’ve made it this far, create a file called “server.go” and enter the following into the file:

package mainimport “net/http”

func main() {
http.ListenAndServe(“:8080”, http.FileServer(http.Dir(“.”)))
}

Done! There literally isn’t any more to do except run the program. Just run the program and it will start the server.

go run server.go

What we have created is a Go program that will serve all files from the current directory on port 8080. Open a browser of your choice and go to [your rpi ip]:8080 if you’re on a separate machine, or just localhost:8080.

go2

And there we have it! I can click on the file to see its contents, or right click to download. As easy as pie!

Closing thoughts

If you aim to do some actual development on the pi, have no despair. Vim has great Go integration. Simply install Vim with the go auto-complete daemon.

apt-get install vim
apt-get install vim-gocomplete

I’ve just started to dip my toes into Go, and I’m really enjoying it so far. I like the syntax, love the simplicity, and simply adore standard library! There is so much to like here, and I can almost claim that this will be the language of choice for my future projects.

Happy hacking!

Leave a Comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.