Learn by Teaching
Since Go is not a blessed language with my current employer, I am always looking for excuses to work with it. Luckily, I was given the opportunity to create a course for Pluralsight that gave me the chance to show what I’ve learned and deepen that knowledge throughout the course-creation process.
In order to solidify and strengthen my understanding of the language, I decided to focus on developing the course by only using the core libraries. While I wasn’t able to do this completely (parameterized routing is not directly supported), I was surprised at how close I could get. Here is a breakdown of the course and what I cover:
Creating a Resource Server
In this module, the basic components of building a web application are explored. I start with a simple HTTP listener and grow the demo code out to serve as a full resource server, providing static files from the server.
Go has a very powerful and flexible template system. So much so that a full exploration of this topic could justify a course by itself (and might someday…). In this module, I explore the basic elements of the template system that allow for simple, yet powerful templates to be used to create the view layer of the application. Among the topics covered are: data binding, branching and looping, and template composition (using templates from within other templates).
Building an Model, View, Controller (MVC) Style Application
Go’s basic library comes as a toolkit, meaning that it exposes a collection of components but doesn’t prescribe how they should be composed together. While there are many third party web-frameworks for Go, I chose to walk through the process of creating an MVC framework from scratch using, whenever possible, only core tools. As a software architect by day, I found this process extremely enjoyable and remarkably easy due to the high quality tools that come in the toolkit.
The View Layer
This module takes the lessons learned with templates and applies them to a demonstration project that was designed to simulate a real world problem (a basic e-commerce site for supplying lemonade stands). At the beginning of the module, all of the pages are served as static HTML. By the end, the pages are fully data driven, using templates and a primitive router to allow the server to dynamically manipulate the content that is served.
The Controller Layer
When creating the view layer, a primitive and poorly factored router is included in the application’s main function to handle serving different pages. In this module, a dedicated controller layer is built, complete with a front controller that serves as the router and a collection of back controllers that serve the requests.
This module is broken up into two parts. At first the controllers just serve GET requests, including parameterized routes. Since parameterized routes are not supported out of the box, the awesome “mux” package from the gorilla toolkit was called upon to fill the gap. The second part of the controller discussion deals with handling data submitted by the user, including posted HTML forms and POST requested sent using Ajax.
The Model Layer
Normally, the model layer of an application holds the specific business rules of the project. As a result, this layer typically ends up being “normal” code for the language, without any specific elements that indicate that it is part of a web application. I take advantage of this pause in the action to introduce an element that I feel is missing in many “build and app” style courses like this – testing. While I don’t go into extensive detail about how to use Go’s testing framework, the system is pretty straight forward and it doesn’t take long to create the foundation for a strong unit testing suite.
The final element of a web application is loading and saving data from persistence layer, typically a database of some sort. I cover this by creating a basic system for logging a user in with an encrypted password and using that information to create a database-tracked session object to allow the user to avoid logging in each time they visit.
Overall, I was impressed with how straight forward and easy it was to create a full web application with Go. As you might expect, it is challenging to have demonstrations that are sophisticated enough to be realistic but simple enough to be easily explained. Go’s simple programming model and focused library allowed me to use a pretty high-level demonstration project, hopefully giving a good context for how you might use Go in your own projects.
If you are interested, check out the course here: http://www.pluralsight.com/courses/creating-web-applications-go and let me know what you think.