Why should you learn Go?

Tram Ho

In recent years, there has been a resurgence of a new programming language Go or Golang . Nothing drives us developers more crazy than a new language, right? So I started learning Go four to five months ago and I’m here to tell you why we should learn the language.

I won’t teach you how to write “Hello world” in this post. There are many other articles that have this content. I will explain the current stage of computer software and why we need programming languages ​​like Go. Because if there are no problems why do we need solutions?

Hardware limitations:

Moore’s Law is getting wrong

The first Pentium 4 processor with 3.0 Ghz clock speed was introduced in 2004 by Intel. Today, the 2016 MacBook Pro has a clock speed of 2.9 Ghz. Hence in nearly a decade, we have gained not much in raw processing power. You can see the comparison of processing power gain over time with the chart below.

From the chart above you can see that the single-thread performance and processor frequency have remained stable for almost a decade. If you thought adding transistors was the solution, you’re probably wrong. Because on a smaller scale some of the quantum properties started to show up, and because it actually took more to add more transistors, the number of transistors you could add started to fail.

Hence the solution to that problem is

  • Manufacturers started adding more processor cores to their processors. Today we have quad-core and octa-core CPUs available

  • Using Hyper Threading
  • Add cache to the processor to increase performance

But the above solutions all have limitations. We cannot always add cache to processors to increase performance because the cache has physical limitations: the larger the cache, the slower it is. Adding more cache to the processors is also more expensive. Multi-core processors can run multiple threads at the same time and bring concurrency to our picture. We will discuss this later.

So if we can’t rely on improvements in hardware, the only way we can do it is to create more efficient software to increase performance.

Go has goroutines

As we discussed earlier, hardware manufacturers have added more processor cores to their processors to increase performance. All data centers are running on those processors, we should expect more processor cores in the coming years. Furthermore, today’s applications are using many micro-services for the maintenance of database connections, message queues, and cache maintenance. Therefore, the software that we are developing and programming languages ​​should support concurrency, and they should be scalable with an increase in processing cores.

But modern programming languages ​​like Python and Java are all single-threaded environments. Most languages ​​support multithreading, but the real problem comes with concurrent implementation, thread-locking, race conditions and deadlock. Those things make it difficult to create multithreaded apps on those languages.

For example, creating a new thread in Java is not memory efficient. Because each thread takes up nearly 1Mb of memory and eventually if you run thousands of threads, they will create enormous amount of pressure on the heap and will force the system to shutdown because of out of memory.

On the other hand, Go was released in 2009 when multi-core processors were available. That’s why Go was built with concurrency as the standard. Go has goroutines instead of threads. They consume almost 2Kb of memory in the heap. So you can make millions of goroutines at any time.

Other benefits:

  • Goroutines have segmented stack that can grow. That means they can use memory only when they need it.
  • Goroutines have faster boot time than threads
  • Goroutines allow you to avoid having to reorder mutex keys when sharing data structures

Go runs directly underneath the hardware

One of the most significant benefits of using C, C ++ over more modern programming languages ​​like Java and Python is their performance. Because both C and C ++ compile and not interpret.

Processors understand binary code. Generally when you build an application that uses Java when you compile your project, it compiles human readable code into bytes of code that can be understood by the JVM. The VM then interprets the bytecode and converts them to processor binaries that can be understood

Meanwhile, C / C ++ does not proceed on the VM and removes a step from the execution loop and increases performance. It directly compiles the code into binary code.

But allocating and clearing those languages’ memory is a huge challenge. Whereas most programming languages ​​handle allocating and deleting them using Garbage Collector or Reference Counting algorithm.

Go has the best of both. Like low level programming languages ​​like C / C ++, Go is a compiler language. That means the performance is nearly identical to those languages. It also uses garbage collector to allocate and clear memory of objects. It’s cool, isn’t it.

The code written by Go is easy to maintain

Go doesn’t have a terrible syntax like many other languages. It is very neat and has a clean syntax.

The Go designers at google had this in mind when they created this language. Because google has a large amount of code and code, it is easy to understand with other developers.

Go deliberately removes many of the features of modern OOP languages.

  • There is no class . Everything is divided into packages. Go has only structs instead of classes
  • Does not support inheritance. This makes the code easy to change. Like in other languages ​​such as Java, Python, if class ABC inherits from class XYZ, class XYZ changes may lead to some consequences for class class XYZ.
  • No constructors
  • No annotations
  • No generics
  • There are no exceptions

The above things make Go very different from other languages ​​and make programming in Go also different. You may have to code more so that the features on that also make your code cleaner and clearer.

The above graph shows that Go is almost as efficient as C / C ++, while keeping the syntax as simple as Ruby, Python.

Go is backed by Google

  • This may not be a direct technical benefit. But Go is designed and powered by Google. Google is one of the largest computing infrastructure in the world. Go was designed by Google to solve their problem in supporting scalability and efficiency. Those are the same issues as you when creating your own server

Conclude :

  • Even though Go is very different from other object-oriented languages, it’s still a beast. Go gives you high performance like C / C ++, super efficient concurrency handling like Java, and fun to write code like Python / Perl.
  • If you don’t have any plan to learn Go, I would still say that the hardware limit pressures us, software developers to write super efficient code. Developers need to understand the hardware and optimize their program accordingly. The optimized software can run on cheaper and slower hardware (like an IoT device) and have a better overall impact on the end user experience.

Refer :

https://medium.com/@kevalpatel2106/why-should-you-learn-go-f607681fad65 https://www.educative.io/courses/a-quick-primer-on-garbage-collection-algorithms/jR8ml

Share the news now

Source : Viblo