At the start of any project one of the many things to consider is the development language or languages.
For a project I’m involved in, whilst wearing my Tirami hat, the software being developed fitted into several components with each component potentially being developed in a different development language.
The part that I’m focusing on is the data storage and querying server. My most recent projects in that area have been Rails based and that was an obvious first choice for me. However given that the actual database schema is very simple and the server API only being required to handle a very small set of methods I thought it was a good opportunity to give something else a try. This led me to looking at Go ( http://golang.org ).
As of writing this sentence I’ve not tried Go yet and it may prove to be a dead end. If it does then I’ve always got the fallback of Rails. So from here on it is a mystery on what I end up using (unless you’re impatient and have scrolled down to the end of this article!) As a jumping off point I foundhttp://tumblr.bwong.net/post/52143548323/howtolearngo and began to work my through what it suggested.
Installing Go on my MacBook Pro running Yosemite was straight forward (having battled some Ruby gem installations earlier in the day for another project this was a nice relaxing install). Within 30 minutes I had everything up and running and successfully worked my way through the first lessons of Go development and even had a test running.
Simple Steps to a Simple Web Server
Seeing as the aim is to develop an API service that outputs and inputs JSON via HTTP the next step was to see how easy it was create a simple web server. It turned out to be very easy indeed and I had something up and running in under 5 minutes.
A search online found http://thenewstack.io/makearestfuljsonapigo/ which proved to be the perfect jumping off point to writing my own RESTful JSON API. Within a couple of hours I had a simple server responding to 2 different URL paths and returning some dummy JSON structured in the format that matched my initial Swagger specification.
Sharing with the Team
Building and running on a development machine is one thing, the next step was to ensure other developers in the team could also download and run the code. Another task that turned out to be refreshingly simple. So now the front end developer can run a version of the server that outputs sample data that they can use to start developing the front end.
Adding a Database
The next step was to decide on the database to be used to store the data internally to the application. Time to explore using Postgres with Go! For this I found another website http://astaxie.gitbooks.io/buildwebapplicationwithgolang/content/en/index.html
It took about 2 hours playing around to get a first pass database setup and interacting with it. I then spent another hour or so refactoring the code and then another 30 minutes or so wiring up the searching so that it returned JSON based on the data for the search term entered as part of the URL received by the server.
A Positive Experience
The code I’ve written may not be pretty and will probably cause some Go diehards to be offended but this is a rapid learning curve and I’m pleased with the results so far in such a short space of time. To recap in the space of about 8 hours spread over a couple of days I’ve gone from having written zero lines of Go to:
● Installing the setup on a Mac
● Working my way through the basic tutorials
● Building a very simple web server
● Building a slightly more advanced RESTful JSON API
● Adding in Postgres for backend data store
● Generating output JSON based on queries submitted via HTTP
Importantly, I’ve also had fun as it feels like writing real code and real software and part of me feels like it should have been done on a green screen 80 column VDU, saved onto a 5 ¼” floppy disk and printed out via a line printer for reference.
Since originally writing this article I have had reason to get a Windows 10 based development laptop for another project. Having had very bad experiences getting open source development environments to run on Windows in the past I must say I was very pleasantly surprised to discover how easy it was to get Go up and running on Windows 10. It only took me around 15 minutes to go from nothing to having my project build and run on Windows 10.
I’ll still be doing my main development on my Macs but it was good to know it can run easily enough on the other side.