Why I am building a blockchain in Go

GoLang has become a go-to language for developing decentralised systems today. Every other organisation is using it for their core processing modules, it has also gained a lot of traction in web development.

We evaluated multiple languages like C, C++, Java, and even NodeJs when we decided to build Karachain. We also tried benchmarking some algorithms to decide the core language for the implementation of Karachain protocol. Today, I am pleased to announce that we are using Go for this very purpose.

Building a blockchain right now is equivalent to building an Operating System way back in time.

We focused on some of the core problems of building something as complex as a blockchain platform. After almost 4 weeks of development and research, we found out how the beauty of this amazing programming language has solved our problems.

Maintainable code in the long run

Go is simple. And, then it has a very less number of quirks that saves you time when you come back to the language after a cessation. Also, there are very few things to learn and a few ways of doing the typical — a quality that greatly reduces the steepness of a developer’s learning curve. This feature, in any language, will attract a huge number of developers. These reasons also facilitate a very low number of chances for bugs. This eventually makes the entire development process simpler and faster, considering the immense delay every bug introduces to any development.

The more lines of code are added to a project, the more difficult it becomes to maintain. Since a blockchain can require thousands of lines of code, we wanted a language which could make the maintenance of such a huge project very easy.

Easy for developers to master

We had a team of 20 awesome developers when we started, most of them came from programming languages like JavaScript, Java, and Python. Some were system engineers good in C as well.

We needed a common programming language to work with, convincing them to learn Go was very easy, but training them to become productive Go developers was easily achieved within a month, i am not sure that we could have achieved this with any other language so soon.

Speed and Efficiency

Unlike Python, Go is not an interpreted language — it is a compiled language. This drastically reduces the overhead of ‘on-the-fly’ errors that happen in interpreted languages. This is similar to C, where the code is compiled first, errors displayed asking to resolve, and only then the code runs; but, it is a much higher-level language than C, and much more productive than JavaScript and Python.

A blockchain needs to be very efficient mainly due to the cryptographic calculations and the huge amount of data it has to propagate as well as store in the network.

It is built for distributed systems

This is already proved by the tools and softwares which have already been built using Go. Docker, the container for microservices is also built using Golang.

We have seen our microservices built using Go easily scale to millions of requests as compared to PHP.

Goroutines

Concurrency is the ability to run several programs or several parts of a program asynchronously or in parallel, which improves the throughput. Usually, threads are used to achieve concurrency in languages such as Java and others. In Go, methods called Goroutines are used. Goroutines are methods or functions that can run with other functions in parallel. Certainly, a Goroutine takes about 4 Kilobytes of space in RAM. On the contrary, a thread takes up about 1024 kilobytes in RAM. Hence, Goroutines take up 250 times less space than threads in other languages, which makes it possible to run an increasing number of Goroutines concurrently.

Following is an explanation of handling of threads differ in Go in comparison to Java:

In Java, objects are shared between units of work, and to access this piece of data, a unit of work must first obtain a lock on it. In Go, Channels are shared between units of work, and a channel is basically of a FIFO pipe — a unit of work may read or write to a channel.

Go adheres to its motto of “not to communicate by sharing memory, instead share memory by communicating.”

And, blockchain has a wide importance of parallel actions, where, this particular feature comes very handy. Also, the ability to run a lot of functions in parallel allows programs written in Go to be run on distributed systems — a primary need of the blockchain technology. Some profound examples where Go has been adopted for its concurrency are Docker, MongoDB, Netflix, Uber etc. — companies for whom concurrency and being able to run in a distributed manner is of prime importance.

Everyone in the blockchain world is using it

Most of the stable blockchain based DApps and tools are built using Golang these days. It is very easy to find required libraries and packages.

Go is a compiled language — hence it runs directly with the OS. This can allow us to build technology like EVM (Ethereum Virtual Machine) in a much better way. Contrary to this, we use Java which runs in its own JVM which then runs on top of the OS. So, building a virtual machine for smart contracts which itself runs on another virtual machine introduces a completely unnecessary level of abstraction. This also reduces performance drastically.

Go gives the feel of a scripting language and has a low startup time, hence is great for small programs. The Queries per second (QPS) is much better in Go than Java. Hence, Go can be used to build systems to handle high volumes of requests.

Some beautiful code examples regarding Go

  1. Here’s how you can simply return a pointer to a local variable from a function:

2. You can write functions inside functions and you can return functions just like in a functional language and the local variables around it serve as variables in a closure:

3. Object Orientation

Go is a very simple, concise, easy-to-learn language whose hallmark is “pragmatism” rather than syntactical beauty. It was designed to solve software engineering problems in a team environment, and in this respect, it does the job admirably.

Ask Us Anything on our Telegram group: https://t.me/karachain

Please visit us to know more about how we are bringing developers and the blockchain closer: www.karachain.io