How Go can be your “Go-to” language

Gunjan Sharma Feb 08 - 7 min read

Audio : Listen to This Blog.

 “Go”, or “Golang”(as it is sometimes referred to), is an open source project developed by Google, to make programmers more productive. In order to understand why it is such an important language today and what promise it holds for the future, we need to understand the reasons that let to its development.

The Hardware Scenario

In the past few decades, there has not been any significant improvement in the CPU processing power. To put things into perspective, Intel launched a Pentium 4 model with a clock speed of 3.06 GHz in November 2002, while the processors commonly used today (after 15 long years), still have a clock speed of around 2.9-3.6 GHz.

In order to meet the demand for more power, manufacturers started increasing the number of cores, since this makes it possible to increase the processing power without increasing the clock speed. Today, we see quadra(4), octa(8) core processors and the like. However, there are limitations to this approach as well.

Limitations of Multi-core Processors

  • Hardware limitations – When more cores are added, the transistor density increases. This, in turn, increases power consumption and heating of the cores. To reduce power dissipation, cores have to run at relatively lower frequency than single-core processors.

To reduce the power heat in hot-cores, voltage and frequency is reduced. This further lowers down the performance.
Further, cores can only be scaled to a certain limit.

  • Cost – Increasing the number of cores increases the cost substantially. After a certain limit, it is not economically feasible for manufacturers.

The present-day hardware is not able to cope-up with the increasing demands for higher processing performance. So, the only way out is to create optimized and efficient software.

 Expectations from programming languages and software

  •  Language should permit efficient code to be written.
  • Software should be written with a deep understanding of the underlying processors.
  • Such programming languages should be preferred, which make it easy to write and manage concurrency and parallelism.
  • Software should be scalable. That is, it should fully utilize the processing power of the hardware, when more cores are added.
  • Language should be compiled, not interpreted (to reduce execution time).

 Limitations of many programming languages

Many of the programming languages available today support multi-threading. However, it is not easy to write multi-threaded programs using them. Further, the amount of resources consumed by multi-threaded programs is a matter of great concern.
As an example, creating new threads in Java is not memory efficient. Each thread consumes approximately 1Mb space on the memory heap. When multiple threads are created, it puts immense pressure on memory resources, sometimes resulting in abrupt termination due to low memory.
C++, another popular programming language, also lacks proper concurrency support at the language level.
Another popular language, Node.js is “single-threaded”. It cannot directly use multi-core CPUs.

How does Go help

As developers, we need to start to thinking more about optimization as well as best-coding and best-development practices. Go is the programming language more suited for this kind of development. 
Go was released in 2009 with the aim of addressing all these concerns that are present in several popular languages today. This language was created, with concurrency as the major goal.
 

Fig. 1 The Go Language

 Major Features of Go

Minimal Memory Consumption

Go has its own abstraction over “threads”, called “goroutines”. Creation of a goroutine takes just 2Kb of heap memory. Each goroutine is automatically and efficiently managed (tasks like adding/removing goroutines are handled efficiently)
The Go runtime ensures that any one goroutine isn’t blocking the others. The code is written in a synchronous style while being fully non-blocking. There is no need for callbacks.

Reduced Setup and Teardown Costs

Threads have significant setup and teardown costs because it has to request resources from the OS and return it once it is done. The workaround to this problem is to maintain a pool of threads. In contrast, goroutines are created and destroyed by the runtime and those operations are pretty cost-effective.

 Low Switching Costs

When a thread blocks, another has to be scheduled in its place. Threads are scheduled preemptively, and during a thread switch, the scheduler needs to save/restore all registers, that is, 16 general purpose registers, PC (Program Counter), SP (Stack Pointer), segment registers, 16 XMM registers, FP coprocessor state, 16 AVX registers, all MSRs etc. This is quite significant when there is rapid switching between threads.
In contrast, goroutines are scheduled cooperatively and when a switch occurs, only 3 registers need to be saved/restored – Program Counter, Stack Pointer and DX. The cost is much lower.

Synchronization and Messaging Feature

Go language has a concept of “channels”. Synchronization between goroutines takes place through channel communication.

Multi-way Concurrent Control

Go supports multi-way concurrent control through “Select”.

Easily Manageable Code

Go has a syntax that resembles C. It is easy to learn and understand.
It is easy to create thousands of goroutines in one program.

Open Source

This means that its source code can be customised efficiently to fit our needs and requirements. Further, Open source programming languages are subject to peer review constantly, so the bugs present in the program code can be easily found and fixed. Users from different programming backgrounds and countries can collaborate and work together.
There are thousands of additional applications and libraries at awesome-go. Some of the most popular ones being Moby (formerly Docker),Kubernetes, Hugo, and Prometheus.

A Sample Go Program

The following simple program demonstrates Go’s concurrency features.
It launches two “goroutines”: one waits for the user to type some text, while the other implements a timeout. The ”select” statement waits for either of these goroutines to send a message to the main routine, and acts on the first message to arrive.

Fig. 2 A sample program in Go

Fastest Growing Language

  • No wonder that Go has been embraced by approximately 1 million users worldwide.
  • Today, it is the 9th most popular language.
  • It is the fastest growing language on GitHub.
  • It is being termed as the language of Cloud infrastructure. Every single cloud company has critical components of their cloud infrastructure implemented in Go including Google Cloud, AWS, Microsoft Azure, Digital Ocean, Heroku and many others.
  • As per the TIOBE Index, Go was 2016’s programming language of the year. Every year, this award goes to ” the programming language that has gained the most popularity in a year.” And “without hardly any competition Go has won the award for 2016,” TIOBE writes.
  • Go is good especially for stand-alone command-line apps or scripts, network and web servers.
  • Google Trends chart highlights the popularity of Go, over the years.

 
Fig. 3 Popularity of Go. Here, numbers represent search interest relative to the highest point on the chart for the given region and time. A value of 100 is the peak popularity for the term.

Conclusion

Hardware isn’t getting significantly faster, anytime soon. However, there is an ever-increasing demand for better software performance. In such a scenario, a language like Go can definitely help. However, that’s not enough. Developers like us need to understand more about the hardware, in order to be able to exploit the full range of multi-core architecture of the CPU. Writing optimized code using a language like Go will make it possible to run software smoothly even on cheaper and slower hardware. This means, for instance, less expensive cloud servers. This, in turn, means higher profits for corporations and overall cost reduction for the end-user. Needless to say, when the hardware is upgraded, the software will run at a blazing speed!
References and Suggested Reading
https://en.wikipedia.org/wiki/Comparison_of_Intel_processors
https://blog.nindalf.com/posts/how-goroutines-work/
https://talks.golang.org/2012/splash.article
https://www.tiobe.com/tiobe-index/
https://github.com/avelino/awesome-go

Leave a Reply

At MSys Technologies, we are constantly seeking to expand our horizons by embracing modern technologies. We are a global technology services company with expertise in Storage, IoT, Cloud Computing, and Analytics.