Go + The Gorilla Toolkit = A Complete Web Development Framework
Go comes out of the box with an excellent set of tools for setting up a basic web application. It contains first class handling for HTTP messages, cookies, headers, and the like, but it was created as a systems programming language, not a web-development language. This has led to some critical pieces of the standard web-development stack being lacking in Go’s base library.
Enter the Gorilla Toolkit
The Gorilla Toolkit attempts to fill in the gaps that exist in Go’s core library by adding in nine packages that provide supplemental functionality. Some of the packages address features that are obviously missing and have become ubiquitous in web application development. Other offer different implementations of features that are already part of the base library, but with a different implementation to get around a limitation.
This package adds parameterized routes to Go’s basic route handling logic. With Go’s basic route handlers, it will find the route that is the best match for a given request and pass the request to that handler for processing however, these routes have to be statically defined. This means that the RESTful route: “/purchaseorders/42” which would presumably get the purchase order with the ID “42”, cannot be handled since the 42 is dynamic and the basic route handler isn’t designed to handle that.
Mux contains an entire suite of route matching logic that allows route handlers to be matched based on a route pattern, information contained in headers, query parameters or just about anything else that can be contained in the header section of an HTTP request.
The gorilla/reverse package addresses the general case of matching a request to a handler. Where the mux package deals specifically with route handling, the reverse package uses matchers to give a Boolean (true or false) value about whether a request matches a route or not. This allows a general section of code to be used to triggered based on the request. This is useful for things like request filters that are monitoring requests, but aren’t the primary handlers of the request.
The reverse package also contains an interesting set of functions that will combine a regular expression with set of parameters and turn it back into a string. This is the reverse of how regular expressions are used and, I think, how the package got its name. While the use cases for this capability are not immediately obvious, there are some very powerful capabilities that this allows, such as generating route templates and hyperlinks from the same basic regular expressions and, hence, ensuring that route patterns are automatically synchronized throughout the application.
Most web applications do not simply handle requests via a single request handler. Instead, additional sections of code, often called “filters” or “middleware” gain access to the HTTP request and can handle different situations that are not the primary responsibility of the request handlers, such as enforcing authentication requirements.
The gorilla/context package allows information to be stored for the lifetime of the request in an object that is keyed to the request itself. This provides a way for the filters to add information into the request stream without becoming too tightly coupled to the other request handlers.
Go comes with good, basic form handling that is bundled into the net/http/Request object. However, processing long forms with a lot of information can get cumbersome. This strategy also doesn’t deal well with the conversion of flat forms into type hierarchies. The gorilla/schema package is designed to provide a turn-key solution to form handling by describing a set of naming conventions for form fields that allows forms to be parsed into complex object graphs complete with slices in just a couple of lines of code.
Arguably, one of the largest features that is missing from Go’s basic library, at least when speaking about web applications, is the ability to secure cookies. Since cookies are often used to store sensitive user information, such as authorization tokens, they are critical points in the security of a web application that must be carefully handled.
Go actually comes with a pretty solid offering around remote-procedure calls and so the gorilla/rpc package does not immediately appear to be necessary. However, this package reimplements RPC as a HTTP round-trip and, thus is able to run in contexts where straight TCP connections aren’t a viable option. Additionally, this package comes with some enhancements that make RPC servers more flexible and able to server more clients.
As a part of the RPC implementation, Gorilla provides two protocols – JSON-RPC and protorpc to send and receive RPC requests. This gives, plus a pluggable model that enables additional protocols to be created, allows a great deal of flexibility when creating RPC servers.
Sessions are perhaps one of the oldest aspects of web applications that Go doesn’t support out of the box. This ability to store information related to a single HTTP client across multiple connections is often used to enforce security requirements and implement e-commerce shopping carts. The gorilla/session package sits on top of the gorilla/context and gorilla/securecookie packages to provide a straightforward and easy to use session management system for Go.
While sessions are a classic tool that is not included in Go, the Gorilla Toolkit also attempts to implement some of the latest features of web applications, such as WebSockets. These special connections basically take an HTTP request and convert it into a persistent TCP connection with a thin wrapper over top of it. This persistence allows the server to send messages back down to the client without being prompted beforehand. This approach allows the server to be much more active in the management of the clients and allows powerful new capabilities such as chat clients and real-time logging.
All in all, Go, when enhanced by the Gorilla Toolkit becomes a powerful web application development platform that can compete with any other web-development language out there.
If you are interested in learning more about Go and the Gorilla Toolkit, checkout Pluralsight where there is not over 20 hours of content about the Go programming language. Specifically, you can watch my new course specifically on the Gorilla Toolkit to learn more about how to integrate Gorilla into you application.