Go and Polymer, So Happy Together

Travis Reeder
Iron.io Technical Blog
4 min readNov 10, 2014

--

I’ve been hearing a lot about Polymer lately, as I’m sure a lot of you have, seeing as how all of Google’s new application redesigns are based on it. Or maybe you’ve heard of Material Design which is Google’s new design philosophy and the Paper Elements are Material Design in action, but underneath is Polymer. So anyways, I had to take it for a spin to see for myself what it’s all about.

Thoughts on Polymer

They built a great tutorial that takes you through the concepts of Polymer; how it works, how to build components, how to do data binding, etc. I found a few things that were really interesting:

  1. Separation of Concerns. It completely separates the backend from the frontend. You don’t generate pages on the backend like most of us are probably used to, the pages are just HTML, CSS and JavaScript which ask a server for data after they load, rather than generating a custom page for your user on the backend while it’s loading. Why is this interesting? Because now you can make a well designed API and use it for your public facing API (for third party usage), AND you can also use it for your main web application (using Polymer), your mobile apps, etc. That may not seem like much, but until now, the typical process was to build your web application that directly accesses your database and renders custom pages for your users, then also build your API for other systems and mobile apps to use. Does this make things like Ruby on Rails obsolete? It just might and the example you’ll see in a bit may show you why. I’m not saying it will make Ruby obsolete, just big MVC web frameworks in general.
  2. Reusable components. You can make your own components, then reuse them throughout your app or in other apps too. There are already a large set of elements you can use that are part of the Polymer project and you can make your own and share them with the world too.
  3. Web/Mobile Interface in One? Maybe we’re near the end of having to build a web app, an Android app, an iOS app, a Windows Phone app, a Blackberry app, etc. Maybe we can now kill two birds (or 5) with one stone. This pipe dream remains to be seen, but it seems like it could be heading this way. I, personally, would love to see this happen. Making apps for multiple platforms is a righteous pain in the ass. Thanks smartphones.

Thoughts on Go

Love it. I’ve been using it heavily for a few years now at Iron.io and it’s become my goto language (pun intended). Go is great in a lot of ways: simple language, strict compiler = less errors, awesome performance, easy deployment, etc.

Serving Up Data With Go

Go has quickly become a force to be reckoned with, especially in the API space. It hasn’t made much of a dent in the web app space though, because there aren’t many good web frameworks for it, and building web apps with it is just kind of painful when compared to something like Ruby on Rails which makes it quite easy. Ruby’s flexibility really pays off when it comes to web development. But what if you could build web apps without needing a nice and easy backend web framework? Or any web framework at all?

Well you don’t really need a web framework with Polymer, you just need a good API. And that is why Polymer and Go fit so nicely together. Go is awesome for making API’s. Polymer is awesome for making nice UI’s that talk to API’s. Boom. Match made in heaven.

Go is awesome for making API’s. Polymer is awesome for making nice UI’s that talk to API’s. Boom. Match made in heaven.

To show this working together, I took the app from the Polymer tutorial and modified it to serve the data using Go. If you go through the tutorial, you’ll see it serves a static json file called posts.json that looks like this.

Here’s a Go program that serves up those post entities Go style: https://github.com/treeder/go-polymer/blob/master/hello.go

The main things are adding the `/posts` route to the http handler and serving up the posts. This program unmarshals the data from the json file, then marshals it back in the response, but the only difference in the real world is you’d read the data from a database somewhere, then marshal it in the same way.

That go program actually serves a dual purpose, it serves up the static files that make up the Polymer frontend and serves the API. You could serve the static files however you want though, it doesn’t have to be part of the same program.

If you want to try running it, just check out the project README: https://github.com/treeder/go-polymer

Live demo: http://go-polymer.appspot.com/

There you have it, a simple example of using Go with Polymer. Hopefully it tweaks your interest in this stuff as it has mine.

--

--