Using [Idiomatic] Go to Stream the NYTimes Newswire

Here’s what I love about the internet. I post my original blog post to Twitter late Saturday night, and by mid-morning Sunday my friend and host of Metacasts.tv-a Go Screencast-Mark Bates, has submitted a pull request tearing apart my Go code. While I’ve written several small Go Apps, I’d never shown my Go to anyone. Turns out, I wasn’t doing anything idiomatic[ly]. Does that make it wrong? No, it works just fine, probably makes little to no difference from a performance standpoint. But being able to directly compare what I produced, and the idiomatic approach, is immensely helpful. So now I present to you a new blog post, where I will attempt to grok the idiomatic approach and explain how it works.

Setting up the Stream

While before I used go-json-rest to create a stream, turns out with Golang as long as you don’t close the connection, it’ll just stream. So that’s about as dead simple as it gets…

The original approach

Creating the StreamHandler

The big change here was by removing go-json-rest, we’re now just writing directly to the HTTP response. We’re initializing a JSON encoder around the HTTP ResponseWriter, and then just encoding the article we want to send down the pipe to it.

Other changes included not initializing variables with var and instead just using :=. I was also using Arrays to hold everything, but Gophers prefer using Structs for these kinds of things.

The original approach:

Fetching Content From the NYTimes

While we’re grabbing the data in basically the same way, by removing the JSON Body type and just decoding the JSON directly into a Struct of all the Articles, we’ve eliminated the processing method. I might at some point want the additional data about the number of records provided by the top level of the JSON, but seeing as that wasn’t useful to me, we’ll leave it as is.  Now when you call fetch_content(), it returns a nice Struct with the 20 latest articles, all in their correct Article type.

The original approach:

The Sorting of the Articles

While before I created an ByUpdatedDate type in order to sort my articles, the more idiomatic approach since I’m only sorting this one way, is to create an Articles type and just have that sort by UpdatedDate.

The original approach:

You can view the updated version here: https://github.com/scottefein/nytimes-streaming-proxy

And that’s what I learned! Big thanks to Mark for helping me out on this.  I invite you to write terribly non-idiomatic code, not care, throw it on the internet, and learn lots of things from all the OCD people out there that will not be able to sleep at night with your code looking the way it does.

One thought on “Using [Idiomatic] Go to Stream the NYTimes Newswire

Leave a Reply

Your email address will not be published. Required fields are marked *