Michael Lanyon's Blog Notes and thoughts from LanyonM

Trying out Go

Comments

Go has been a popular language for a while and the chatter has gotten to the point where I needed to give it a spin to see what all the fuss is about. The night before a flight to SFO, I installed the Go distro and found the wiki tutorial. I also made sure to get the Go package installed for Sublime so I wasn’t stuck in black and white.

First, I’ll start with the disclaimer that I’ve been using higher level languages like Groovy and Ruby recently, so I haven’t been writing code that gets quite so explicit. The first thing that stuck me was the use of := for assignments. For example:

filename := title + ".txt"

Turns out that’s just the shorthand for var filename = title + ".txt", which is actually receiving it’s type from the assignment in the expression.

The next several steps in the tutorial were pretty self explanatory. The syntax for the way functions can return multiple variables was intuitive, and I really liked how the compiler complains when you don’t use imported packages. The html/template package’s Must function was also a handy way of ensuring that expected resources are present at startup.

The last language feature that the wiki tutorial covered was function literals and closures. Groovy is littered with closures, but I have most frequently used them to perform operations on items in a collection:

// data is: [[name:red, hue:#f00], [name:green, hue:#0f0], [name:blue, hue:#00f]]
def colors = data.collect {
  ["$it.name": it.hue]
}
println colors
// [[red:#f00], [green:#0f0], [blue:#00f]]

The Go closure from the wiki does something a bit more interesting. It wraps a controller action (also a function) with the functionality to validate that the requested resource matches a validator - in this case defined elsewhere as a regex:

func makeHandler(fn func (http.ResponseWriter, *http.Request, string)) http.HandlerFunc {
  return func(w http.ResponseWriter, r *http.Request) {
    // do the work
    title := r.URL.Path[lenPath:]
    if !titleValidator.MatchString(title) {
      http.NotFound(w, r)
      return
    }
    fn(w, r, title)
  }
}

Pretty cool stuff. After the plane ride I did the other tasks suggested by the tutorial and added Twitter Bootstrap to make it prettier. I also watched a video about the origins of Go and why Google invented it. The video helped make sense of some of the languages quirks.

I don’t yet see a clear fit for Go in any of my current work. Our core deliverables are highly branded websites, and Go certainly wasn’t created with the goal of assisting in that development process. In my limited development capacity, I’ve been creating better observability inside the application or providing the tools to create meaningful visualizations with all of our metrics. We have the intention of collection RUM data in the future and maybe Go would be a good way to collect that data. Additionally, projects like gor catch my eye because it would fill a gap we’re looking to fill in the future.