Go Programming and Why should you learn Go?


Go or Golang is a open source programming language created at google by Robert Griesemer , Rob Pike and Ken Thompson in 2009. It is compiled and statically typed like ‘C’ with garbage collection. It is used by some of google’s production systems.


Go is different

It is not what you think; it is not java; it’s not like ruby or python and it doesn’t try to be like them at all it comes closest to ‘C’ than any other language. Go does not follow any patterns like object oriented or functional programing etc.

Go’s purpose is therefore not to do research into programming language design; it is to improve the working environment for its designers and their coworkers. Go is more about software engineering than programming language research. Or to rephrase, it is about language design in the service of software engineering. – Rob Pike


Why learn go ?

Go was not built to make programmers lives better it’s more about software engineering; Go focuses on simplicity, conciseness, readability ,concurrency and performance. Following are some of the features and reasons why you should learn go.


Asynchronous : The golang code is written in a synchronous style while being truly non blocking ie: asynchronous, simple and readable. The go runtime ensures that any piece of code is not blocking the other code ie: Go makes sure that any one Goroutine is not blocking others.


Fig: Software Architecture

Servers that implements thread per client model can be confounded when pooled threads spend too much time waiting on blocking operations like I/O. So  there is no need for callbacks so no callback hell.


Multicore:  The performance on the server side is dictated by adding more cpu cores but not every application is able to exploit multi core for better performance nowadays hardware manufacturers are adding more and more cores to the processors; we should expect increase in number of cores in upcoming years so we can say that Go is future proof as it can exploit multi core no matter how large the number of cores is go is scalable.

Go has one straightforward approach it multiplexes Goroutines over OS threads. Goroutine is a lightweight thread or program managed by Go runtime. Go not only solves C10K problem it blows it away with C1000K.

C10K problem is the inability of the server to scale beyond 10,000 connections or clients due to resource exhaustion.


Concurrency : Executing more than one task simultaneously and making progress is known as concurrency. Go has rich support for concurrency using goroutines and channels.

A goroutine is a function capable of running concurrently with other functions. We can easily create and execute thousands of Goroutines.

A channel  is a way for two Goroutines to communicate with each other and synchronize their execution.

Advantages of Goroutines:  

  1. Use more memory only when needed.
  2. Faster startup time than threads
  3. Communicate with each other using channels
  4. Allows you to avoid having to resort to mutex locking when sharing data structures
  5. One goroutine can run on multiple OS threads. They are multiplexed into small number of OS threads.

You can see Rob Pike’s excellent talk concurrency is not parallelism to get more deep understanding on this. This makes Go very powerful and can handle it like java, c ,c++ while keeping the code clean and beautiful.


Above figure shows where Go resides compared to other programming languages


Static Binaries:  Go does not have an interpreter or a virtual machine that some other languages have like Java has JVM (Java Virtual Machine), Cpython for Python and MRI (Matz Ruby Interpreter) for Ruby.

Cpython and MRI still have GIL(Global Interpreter Lock).It is used to synchronize the execution of threads so that only one native thread can execute at a time. An interpreter which uses GIL always executes only one thread at a time even if it runs on multicore.Go neither have a interpreter nor a virtual machine so there’s no global lock so it can use multi cores.  

JVM based languages have slow startup time this doesn’t just affect auto scaling and server restarts it can also be crippling the development. Go applications compile quickly and launch immediately as Go runs directly on the underlying hardware because there are no other overheads.

In Java or JVM based languages when you compile the code it gets converted  from human readable code to bytecode which can be understood by JVM or other virtual machines thus VM interprets this byte code and convert it to binaries for the processor to understand.


Fig: Execution of VM based languages

C,C++ and GO does not execute on VM it directly compiles human readable code and convert it to binaries this removes a step from execution cycle thus increase the performance.



Garbage Collector: Go has its own garbage collector which takes care of bookkeeping of resources and is used to allocate and remove the object. As object gets passed among threads it becomes unmanageable to guarantee they become freed safely thus automatic garbage collection makes concurrent code very easy to write.

Apart from concurrency garbage collector makes interfaces simpler as they don’t need to specify how memory is managed across them.



Now we can say that Go is very different from other languages still it provides performance like C, C++ and concurrency like Java, Python It’s the best of both worlds.


Use Cases: