Introduction to Go Programming Language — Part 2
This post is the next part of Introduction to Go Programming Language — Part 1. At this post, we will see in more detail about advantages of Go.
The Advantages
1. High Support on Concurrency
One of Go’s great advantage over other programming languages is that its high support on concurrency. Go’s concurrency style is based on CSP (Communicating Sequential Processes), described by Tony Hoare in 1978. CSP itself is a formal language that describe patterns of interaction in concurrent system. Hence, Go uses channels as way to do communication. Then I ask myself “why did they use CSP as the way to make concurrent system?”. These are some reasons that I found:
- Google programming language developers already familiar with the concept
- Easy to add the concept to procedural language without change the model and add much constraints
- Successful model for providing high-level support for concurrency, the higher level it has then the easier it is to be used
To realize the high support on concurrency, Go introduces some new terms: goroutine and channel. Go uses them to do primitive concurrency style. Traditional multithreading model like Java, C++, and Python will require threads to communicate each other using shared memory. Shared data structure in that memory is protected by lock. Threads then will race to get first place to use the resource.
Do not communicate by sharing memory; instead, share memory by communicating.
Rather doing such a thing that let the issue rises, Go use concept of CSP. In this case, channel is used as media to share reference of data between goroutines.
Goroutine
Simply, goroutine is light-weight thread.
- While thread in another programming language takes around 1MB, goroutine only take around 2KB of memory (it contains only 3 registers data, while thread may contain around 16 registers).
- Goroutines (in user space) are multiplexed into OS threads (kernel space) rather than to have 1:1 mapping. Some goroutines will be associated to a OS thread. An OS thread will execute a goroutine. If the goroutine is blocked, then scheduler will swap/switch for another associated goroutine to be executed by the OS thread. A goroutine may be blocked because of it is waiting to send/receive a data through a channel, blocking syscall (file and network operation), and garbage collection cycle process
- Goroutine cost is cheap. While thread is high cost to be created and switched since the apps need to request to OS for threads. However the solution is to maintain thread pool which contain available/reserved threads for apps. On the other side, goroutine has low cost on creation, switching, and destroying. It is even created and destroyed at runtime.
By goroutine, programmer does not deal with thread and OS does not know the existence of goroutine. Programmer also cannot manage amount of threads that will be used in runtime, however we can manage the amount of processor cores used by program by setting variable GOMAXPROCS.
Channel
Channel is method that let goroutines to communicate each other.
Goroutine sends and receives data through a channel. There are 2 types of channel, those are buffered and unbuffered. Unbuffered channel only fit 1 data at a time, while unbuffered channel could fit more than 1 data at a time. Once goroutine sends a data through unbuffered channel, it is blocked until another goroutine receives the data. But if goroutine sends a data through buffered channel, it is blocked if the buffer is full. On the other side, receiver goroutine is blocked if there is no data is sent.
2. Easy to Learn
The purpose of Go is to increase productivity. Therefore, Go offers easiness for developers to learn and code. You could easily understand new syntax in minutes and create an application in hours. In term of teamwork, it boosts your collaboration in a project. It will cut your team’s time cost to learn the language itself, to learn existing project, to do enhancement, to do troubleshooting, and even to build an application from scratch. Knowing this fact, it is not surprising if many startups use Go as its backend services due to their demand for high productivity.
3. Speed Compile Time
Go’s commitment to realize higher productivity is also supported by its compiler. Go at the first time is aimed to have fast compiled time as it wants to combine the advantage of interpreted language that no need time cost for building/compiling. The first approach it has is that Go’s compiler will check for unused packages/dependencies. It will throw compile error if there is any unused package. Therefore, it helps us to ensure that only needed packages are processed that finally increase the build time.
The second approach is that Go source code is compiled directly to binary for specified OS. Hence, it makes the compilation faster rather than a programming language that uses VM. Another reason why Go has faster compile time than the others is that its great dependency management. You could check it out in more detail through its official documentation.
5. One Independent Executable File
Go is said to be a platform-independent programming language. The source code is compiled into as small as possible binary that let Go running without the need for another dependency. If a compiled Java program requires JVM to run, Go only requires himself to run. To support various OS, Go has libraries for its supported operating systems as target OS for cross-compiling. We could specify the target OS in term of configuration while compiling (e.g. via CLI).
6. Complete Enough Standard Library
As my previous post has said, the main focus of Go is to build concurrent and scalable web services. Older programming language like Java has Spring and Spring Boot framework that help your day to create a web service. However, it would be much harder if you are going to work with plain Java. On the other hand, core libraries to build a web service (e.g. HTTP fetching to handle web request and encryption) have been provided as standard libraries in Go. As far as I know, the standard is quite complete and reliable even a unicorn startup in Indonesia uses Go as backend without web framework to serve millions of people.
7. Automatic and Low Latency Garbage Collection
Go is designed GC not only for 2015, but for 2025 and beyond
It is quoted from Go’s official blog. Go is designed to reduce dramatically garbage collection (GC) latency by using algorithm from decades ago. Go’s GC new algorithm is based on Djikstra’s proposal in 1978. Google believes that the algorithm is well suited for nowadays and future hardware capability and latency requirement of software.
We have learned briefly about some advantages that Go has as nowadays programming language. I hope you find it useful to give you an insight or to broaden your knowledge.
Disclaimer : This article is written based on my understanding and quoted from many resources which I mention below. I open to critics, appreciations, and suggestions. Let me know what you think by write your comment below.
References