Getting started with Go

image

I’ve been using Python for a quite a few years as my main go-to language for building web apps, RESTful API’s, utilities, and much more. It’s the driving power for most of our backends at our startup: Blimp, Blimp Boards, and FilePreviews. I have very few bad things I could argue about Python, but I won’t.

I’ve recently felt the need to dig into newer programming languages and technologies. One of the languages I’ve been wanting to try out for a while now is Go. So I did, and here’s why and a couple of resources that helped me out. The first thing I did was read up about Go for a few days.

The Go programming language is an open source project to make programmers more productive.

Go is expressive, concise, clean, and efficient. Its concurrency mechanisms make it easy to write programs that get the most out of multicore and networked machines, while its novel type system enables flexible and modular program construction. Go compiles quickly to machine code yet has the convenience of garbage collection and the power of run-time reflection. It’s a fast, statically typed, compiled language that feels like a dynamically typed, interpreted language.

This perfectly describes Go.

  • It’s pretty easy to understand for new developers, fast and with a good toolset for debugging and performance tuning.
  • Statically typed and compiled you end up with fewer bugs
  • Easy to profile for speed and memory leaks
  • Built-in code formatting
  • Small memory footprint
  • Simple language design
  • Natively multithreaded
  • Built for and actively developed by Google
  • And more…

A Tour of Go

A great resource to get started with Go is A Tour of GoThe interactive tour is divided into three sections: basic concepts, methods and interfaces, and concurrency, that can be compiled and ran right from the browser.

Go by Example

Go by Example is a hands-on introduction to Go using annotated example programs by Mark McGranaghan.

Go Tool

Go is a tool for managing Go source code. Available go commands are:

  • build – compiles packages and dependencies
  • clean – removes object files
  • env – prints Go environment information
  • fix – runs go tool fix on packages
  • fmt – runs gofmt on package sources
  • get – downloads and installs packages and dependencies
  • install – compiles and installs packages and dependencies
  • list – lists packages
  • run – compiles and runs Go program
  • test – tests packages
  • tool – runs specified go tool
  • version – prints Go version
  • vet – runs go tool vet on packages

Godoc

Godoc extracts and generates documentation for Go programs, it parses Go source code and produces documentation as HTML or plain text. After generating your docs you can use GoDoc to host it. You can read more in the Godoc: documenting Go code blog post.

Package Management

With the go get command you can install remote packages directly from version control. One of the things you’ll notice when starting out with Go is that there isn’t a builtin package manager tool like we’d see with package managers like BundlerPip and NPM. There are third party tools for for managing Go packages and their dependencies. The Go project recommends vendoring, taking the 3rd party source code that is referenced in your project and making a copy of that code inside a new folder within the project.

Check out godep, a well-maintained tool for managing vendored dependencies. Some additional resources:

GoUsers

There quite a couple of startups and organizations using Go that blog about or open source packages and tools. A couple of my favorites are:

Useful resources

Discovering projects and packages

Package discovery has been a pain for me. Since there’s no main index, packages can be hosted many different places. You’ll quickly notice that you won’t really need that many external packages, but when you do you’ll be searching around.

  • Awesome GoA curated list of awesome Go frameworks, libraries and software
  • Projects – A list of Go projects
  • Go Search – A search engine specifically designed for Go
  • Go Walker – Displays API documentation for Go projects
  • Sourcegraph – Shows you real examples of how functions and classes are used by other open-source projects.

I’m still learning the best way I know. I’ve already started a new project that I’ll be launching in a few weeks, developed a few packages in the way that I’ll be open sourcing soon.

What resources have you found useful while working with Go? Please share them in the comments below.

Update

Mentioned resources by others.

Getting started with Ember.js

image

I played around with Ember.js on and off last year and after building some quick demos I decided to invest some real time in building real web apps with it. I have been keeping track of some useful tips and resources for people starting out with Ember.js.

Ember.js Guides

The official Ember.js Guides and Tutorials walks you through the very basic concepts for someone starting out, and slowly getting into more “advanced” concepts and features. Those guides are truly written in a simple to understand tone only using technical jargon where necessary. I definitely suggest going over these guides before starting out with Ember. I’ve kept them handy since they are also useful for future reference.

The guides also have a very useful section, Cookbook. It provides answers and solutions to common Ember questions and problems.

Ember.js API

The Ember.js API is usually my second go-to website. It’s very useful to learn about the different available classes that make up Ember. It contains usage examples for methods, properties, and events. They also link directly to where they are defined, if you are into checking out the source code.

Ember CLI

Ember CLI is an Ember.js command line utility. It provides an ideal project structure based on Ember App Kit. For building anything more than a simple demo using Ember CLI makes a lot of sense and makes building with Ember way easier.

Useful content

Learning by doing

I’ve always found that the best way to get started with new technology is to pick a project and just work on it.

Learning from others

Learning from other people’s work is also very helpful. There are many people that are really active on the Ember.js community either because they are part of the core team or just love the framework and contribute back to other open source projects. When in doubt check out some of these influential organization’s/individual’s open source projects.

Getting Help

The Ember community uses StackOverflow to track questions. So its a good rule of thumb to search on SO before asking around. You can also join the Ember.js Discussion Forum which is usually a good place to discuss features and best practices.

The IRC channel #emberjs on Freenode is usually very active and is a good place to find people that might help you out. Apart from being polite, it’s usually a good idea to have code that you can show, so others can reproduce your problem. Don’t copy code on IRC, instead use something like JS Bin.

Let me know if you have any other useful tips, resources, etc…

Barcamp Viejo San Juan

image

Last Barcamp we organized was back in 2012 in Mayagüez. The one before that was in 2011, and the first one was back in 2010.

We already have a date for the next Barcamp Viejo San Juan, Tuesday, June 3 at 7PM. This time we’ll be meeting at Piloto151 in Old San Juan.

For those that don’t know what a Barcamp is, Barcamp is a conference in which the guests become the event speakers. We expect everybody to make a 10 minute presentation to the rest of the audience. The idea is to encourage the exchange of ideas in an open and interactive environment.

Even though the ideal situation would be that everybody gets a chance to present, we know it’s not possible. We’ll have 20 reserved spaces and they will be available on a first come-first served basis.  If there’s not enough time or you are not ready to present, the least we expect is for everybody to interact with questions, suggestions, and help create an environment of interaction with the presenters.

The format for presentations will be 10 minutes and focused around the topics like:

  • Web development & design
  • iPhone / iPad application design & development
  • Software development in general
  • Mobile application design & development
  • Interaction Design, information architecture, user experience
  • Tech startups & venture capital

Spots for presenting are limited so if you’d like to present you should arrive early. Like always, we’ll do everything possible to make this event another #win.

We’ll have beers, some hors d’oeuvres, and t-shirts. Please let us know you plan to attend by RSVP’ing on the Eventbrite event page. This helps us plan for space and food accordingly. 

Internet Speed Tests on a Raspberry Pi

image

A couple of weeks ago soynerdito soynerdito shared a bot he made to tweet internet connection speed results every now and then.

I then remembered I had a Raspberry Pi I wasn’t using at all. So I built a small app that runs speed tests every 3 hours. It’s based on speedtest-cli which is a Command line interface for testing internet bandwidth using speedtest.net.

The only difference with the one I built is that instead of tweeting the results, it posted all available data to an endpoint. I built a Flask app that can be used to store those results for different users as well as share them via a JSON API.

If you’re interested in running your own, check this Gist out. It contains a README file that will help you setup your box(doesn’t have to be a Raspberry Pi). It also includes a sample Flask app to store your own results. If you’re interested in using the one I’m hosting you could do so, just let me know and I’ll send you a URL to store results and one to share or consume. It’d be interesting to see different kinds of results at a larger scale, not just my own.

Here are the results graphed since I started running it. Interesting to see how so many spikes in my download speeds. Those are probably related to download in other computers or probably streaming Netflix. Upload speed as expected doesn’t really fluctuate that much.

Photo credit: John Talbot

The easiest way to add WebSockets to Django

image

TL;DR – I came up with a very simple solution to handle WebSockets in Django apps. You just install django-websocket-request, run a script and you have WebSockets for your Django app. The cool thing is that this solution makes Django believe its getting a (somewhat) normal HTTP request so you can reuse almost all of you application’s code. Plays nicely with Django REST Framework and with plain function-based views and class-based views. Check out the demo and it’s source code on GitHub.

The Details

We’re building Blimp 2 which means a lot of changes in our current application, infrastructure, and how we’d tackle old and new problems. One of the decisions we made regarding our application was how our frontend and backend would be organized and how they would interact.

Blimp’s backend is currently built on Django. It serves our frontend’s HTML, handles our private and public API, and all our business logic. That’s how most web apps are usually built, but Blimp is already a client-side JavaScript heavy application. Right now a normal request goes something like: You request a URL, there’s some backend work, queries to the database, caching, some more logic, render an HTML response, load third party CSS and JavaScript libraries, load our JavaScript application, some more logic, and finally render it.

After months of usage and growth, we’ve noticed a few key improvements we can build on. The new version will have a backend app that will only serve JSON, no HTML. Our frontend app, served from another location, will then consume data from that API. We want the frontend app to use WebSockets when possible, falling back to XHR.

Web frameworks like Django are built for the HTTP request/response life cycle, so everything from middleware, authentication, and views takes in an HTTP request, and at the end is an HTTP response. On the other hand, a WebSockets server implementation knows nothing about that kind of lifecycle and what it means.

Our main objectives were:

  1. Reuse the same serialization and deserialization logic between the HTTP and WebSockets API.
  2. Reuse all business logic for all available resources.

First ideas that came to mind were to abstract all of what we wanted to reuse by writing them as methods for our models. That way we would write what we needed once, sharing it between the two implementations. This seemed like the right answer at the moment, but after a couple of hours, it proved not to be. Since we are building on top of the awesome Django REST framework, we were going to have to subclass tons of their Generic Views and Mixins, which didn’t sound as bad at the moment, but still being very skeptical we decided to look for other possible solutions.

By then we knew that we wanted a single REST API available via two different transports, HTTP and WebSockets. The best case scenario for me was to avoid rewriting anything of what we have already working for the new HTTP API, just to have it work via WebSockets. Then it clicked. I remembered that Sails.js does something similar to what we wanted to achieve.

Sails supports transport agnostic routing, which allows your controllers/policies to automatically handle Socket.io / WebSocket messages. In the past, you’d have to maintain a separate code base to make that happen.

So in more formal terms, we wanted to support transport agnostic routing, allowing us to use everything in Django’s request/response lifecycle to automatically handle WebSocket messages.

Solution

image

WebSocketRequest is the surprisingly simple solution I came up with to solve this.

WebSocketRequest is a simple class that requires a JSON string containing the following keys: method, url, data, and token. The method key can be any HTTP method: GET, POST, PUT, DELETE, PATCH, HEAD, or OPTIONS. The url key is an absolute URL without the domain name. The data key is an optional dictionary in which the key-value pairs in used to create the method’s data payload. The token key is also optional, and used to recreate an HTTP Authorization header, Authorization: JWT YOUR_TOKEN_HERE. You might want to check out my last blog post to learn more about authentication with JSON Web Tokens and if you’re using Django REST framework, you might like to check out django-rest-framework-jwt.

WebSocketRequest works as following:

  1. Validates JSON string payload.
  2. Creates an instance of Django’s RequestFactory.
  3. Dynamically calls one of RequestFactory’s methods, which returns a WSGIRequest object
  4. Resolves the given URL
  5. Instantiates the resolved view passing along the request, any positional and keyword arguments from the resolved URL.

Yeah, RequestFactory, you read that right. You might be familiar with it if you’ve written tests in Django, but if you haven’t, the RequestFactory provides a way to generate a request instance that can be used as the first argument for any view. The only down side to this is that it does not support middleware, which might be a problem for some.

You can install WebSocketRequest via pip.

pip install django-websocket-request

As always source is available on GitHub.

Demo time!

I’ve setup a demo application running on Heroku. Source code is also available on GitHub.

http://dwr-example.herokuapp.com/

Note that Django is not running at all. Tornado is serving a static HTML file and is routing the websocket requests. django-websocket-request then does the magic.

Throttling is enabled and snippets are erased periodically. If you run into any errors feel free to bother me on Twitter about it.

I definitely would like to hear about possible problems of this solution. Where do you think that this fails? How about for a production environment? Where can it be improved?

Photo Credit: Alice Bartlett / Martin Börjesson