December 19, 2018

Gopher Success: The ins and outs of Golang

The story goes that Google’s open-source programming language, Go - or Golang - owes its inception to the long wait-time involved in compiling programs. Tired of the wait and realizing that the software built at Google was not always well served by the languages available to them, three programmers at Google, Robert Griesemer, Ken Thompson and Rob Pike, decided to create their own programming language. The resulting programming progeny is Golang, an open-source, systems-level language capable of bundling efficient compilation, efficient execution, and ease of programming within one language structure.

The fastest growing programming language of the year at GitHub and currently ranked 10th in the TIOBE index of the most popular programming languages, Go is making its presence felt amongst the more established programming languages, C++, Python, Java, C#, and so on. Let’s have a look at some of the reasons Go is rapidly becoming a favorite of beleaguered program developers and our own build choice here at Bug Replay.

Picking up the garbage - key features of Golang

Sporting a modified Hindley/Milner inference technique, Go is a strongly and statically typed compiled language, much like C++ but with the accessibility of dynamically typed languages like Python. Its syntax is reminiscent of the C family, but with only 25 keywords and, like C, is a value-oriented rather than reference-oriented language.

Static code analysis isn’t new but Go kicks it up a notch. With type safety, Go detects compiler errors before execution, sidestepping the need to check types dynamically while executing results in better performance at runtime. Similarly, as a compiled language, Go parses during the build-stage rather than interpreting on-the-fly. Again, the result is faster runtime performance.

Boasting low-latency garbage collection, which translates to efficient and concurrent automatic memory management, Go is designed to build powerful, large-scale software that can scale to meet hardware requirements now and in the future. Go future-proofs its design using a concurrent, tri-color, mark-sweep algorithm, first proposed by Edsger W. Dijkstra decades ago. This is a marked divergence from the customary enterprise-grade garbage collectors but means that garbage collection, the bane of most programmers professional lives, is never an obstacle to creating scalable software

Google recently expanded Go’s powerful package library with the creation of GoCloud. Along with a generic set of APIs to write simpler, more portable applications, GoCloud offers a set of libraries that makes it even easier for developers to create cloud-based applications with Go. Google’s overall aim here it would seem, is to position Go as the foremost programming language for application development in the cloud.  

Go uses the code analyzing tool, GoDoc, to parse source code and produce beautiful, simplified user documentation that evolves in tandem with the code. And amazingly, GoDoc doesn’t use any extra languages, like JavaDoc, PHPDoc, or JSDoc to annotate constructions in the code, just English.

With an impressive array of features, Go is as popular for what it doesn’t have as what it does. Choosing composition over inheritance, Go decouples intent from definitions, meaning that software scales organically without concern for preset hierarchies. With its innovative interface (set of methods) system, Go eliminates subclasses and type-based inheritance. Whenever a type implements its methods, the interface is implemented implicitly—no explicit declaration of intent is required. Just another nifty feature!

Living in a multicore world: the Golang response

In the past decade processors have gained little in power and speed. The practical clock speed of a single core processor hasn’t changed much since Intel’s Pentium 4 processor reached 3.0 GHz in 2004. Solutions, such as increased cache, adding quad and octa-core CPUs, and hyperthreading have all proved limited. It’s clear that developers can’t rely on hardware improvement alone. Therefore, the primary means of increasing performance is more efficient software.

Most programming languages (Java, Python, and so on) are designed for ’90s single-threaded environments. Most support multi-threading, but concurrent execution, threading-locking, race conditions, and deadlocks complicate multi-thread execution.

As a relatively new language, Go was developed in the era of the multi-processor, with concurrency very much at its core, and using goroutines and channels instead of threads consuming almost 2KB memory. Goroutines are functions that can run concurrently with other methods and functions, while channels are built-in primitives that enable synchronized, safe execution of two or more goroutines at any time. The result is the ability to spin millions of goroutines at any time with a much faster, lighter, and more scalable deployment than Java threads. Simply put, Go helps you to maximize CPU horsepower by making it easy to design programs that run concurrently across multiple cores.

To Go or not to Go?

Already quite a mature package, once you install Go you can begin building production-grade software to cover a wide range of use cases from Rest APIs to encryption software before you need to consider third party packages. Compiling to a single native binary, deploying an application written in Go is a simple matter of copying the application file to the destination server; however, with its strict rules and a somewhat underdeveloped library set, Go may not yet be a Python or JavaScript killer.

A fairly major limitation that many point to is Go’s lack of generics, meaning a decreased level of reuse in your code. Go can also make it difficult to determine with certainty whether a struct has implemented an interface without first attempting to compile the program. Yet another complaint that has been leveled at Go is its approach to error handling, which some find less than fulsome. Experienced programmers may find it difficult to shift mindset from classic, object-oriented languages such as Java and C++ to start thinking of things in the Go way. Here at BugReplay, we’re particularly appreciative of Golang’s development processes. Developers have to be very attentive and accurate to make code neat and safe, which in our opinion makes the mind-shift worth the effort.

It must be borne in mind that Go is in its infancy and many issues that developers are currently unhappy with may well be rectified in the fullness of time. What seems certain is that Golang is going places. With a mounting tide of adoption, Go has been battle-tested on such notable projects as Google’s Kubernetes platform, Dropbox, Malwarebytes, Hootsuite, and Basecamp, to name a few. It is quickly becoming a key component in the development of cloud infrastructure, particularly useful for projects involving distributed networks and other complex back-end technologies. So, if you’re thinking of getting into serverless and cloud infrastructures, now might be time to gopher it!