📜 ⬆️ ⬇️

Why should you go?

Hello! I present to you my translation of the article (including graphics) by Keval Patel Why should you learn Go? The article contains many interesting facts that, in my opinion, would be useful to learn for newcomers to Go.

PS: The article, like most articles, in my opinion, reflects the experience and opinion of the author, which may not coincide with yours, and this is normal - how many people, so many opinions.
Gophers

The image is taken from the resource kirael-art.deviantart.com/art/Go-lang-Mascot-458285682

“Go will be the server language of the future.” - Tobias Lütke, Shopify

Over the past couple of years there has been a powerful growth of a new programming language: Go, or Golang . Agree, nothing can drive the developer’s mind more than a new programming language. Therefore, I started learning Go 4-5 months ago and this is what I can say about why you should learn this new language.

In this article I am not going to teach you how to write "Hello, world !!". There are many other articles about this. I'm going to clarify the current state of computer hardware and software and why do you need a new language like Go. After all, if there are no problems, then solutions are not needed, right?
')

Iron Restrictions:


Moore's law does not work.

The first Pentium 4 processor with a clock speed of 3.0 GHz was introduced in 2004 by Intel. Today, my Macbook Pro 2016 has a clock speed of 2.9 GHz. Thus, for nearly a decade, there has not been much progress in pure computing power. Below on the graph, you can see a comparative graph of the growth of computing power over time.

Comparative graph of the growth of computing power over time

According to the graphics, it can be understood that single-threaded performance and processor frequency remained stable for almost a decade. If you think that adding more transistors is a good decision, then you are mistaken. This is because, on a smaller scale, the quantum properties of an object (such as tunneling, or a tunnel effect. - approx. Trans. )) Begin to appear, and adding more transistors can actually lead to additional costs ( why? ) And the number of transistors. , which you can add for one dollar is rapidly beginning to fall.

Therefore, to solve the problem above,


But the solutions above also have their limitations. We cannot add more cache to the processor, because the cache has physical limitations: the larger the cache, the slower it becomes. Adding more cores to the processor also comes at a price. In addition, it is impossible to do it indefinitely. All these multi-core processors can run multiple threads at the same time and this makes our picture multitasking. We will talk about it later.

So, since we cannot rely on improved hardware, the only way out of this situation is more efficient software. But, unfortunately, modern programming languages ​​are not as effective.

“Modern processors look like fun nitro-filled cars — they are excellent in a quarter-mile race. Unfortunately, modern programming languages ​​like the Monte Carlo circuit are full of twists and turns. ”- David Ungar

In Go there are gorutiny !!


As mentioned earlier, hardware manufacturers are adding more and more cores to the processor to increase its performance. All data centers are running on these processors and we should expect an increase in the number of cores in the coming years. Moreover, modern applications use many microservices to support database connections, message queues, and caching. Thus, the software and programming languages ​​that we develop should easily support multitasking ( concurrency. - approx. Transl. ) And they should be scalable with increasing number of cores.

But most modern programming languages ​​(such as Java, Python) come from a single-threaded 90s development environment. Although most of these languages ​​support multithreading, the real problem is the simultaneous execution, blocking of the stream, race condition and deadlock. These things complicate the creation of a multi-threaded application in the above languages.

For example, creating a new thread in Java uses memory inefficiently. Since each thread consumes about 1MB of memory from the heap ( dynamically allocated memory. - approx. Transl. ) And in the end, if you run thousands of threads, they will put tremendous pressure on the memory and may cause the application to shut down due to its lack. In addition, if you want two or more streams to communicate with each other, then it will be quite difficult to do.

On the other hand, Go was released in 2009, when multi-core processors were already available. That's why Go was created with multitasking in mind ( concurrency. - approx. Transl. ). Go uses gorutiny instead of streams. They consume almost 2KB of memory from the heap. Therefore, you can scroll through millions of gorutin at any time.

How do gorutiny work?

How do gorutiny work? Read more: golangtutorials.blogspot.in/2011/06/goroutines.html

Other benefits:


You can watch Rob Pike's excellent performance. Multitasking - this is not concurrency to understand this topic more deeply.

All of the above points make Go very powerful for multitasking in the same way as in Java, C and C ++, while at the same time preserving the beauty and naturalness of the code, as in Erlang.

Multitasking versus efficiency graph
Go has absorbed the best of both worlds. At the same time easy to write and effective in managing multitasking

Go runs directly on the hardware.


One of the most significant advantages of using C / C ++ instead of modern languages, such as Java / Python, is their performance. Because C / C ++ is compiled, not interpreted.

Processors only understand binary code. As a rule, when you write an application in Java or other JVM-based languages, when you compile your project, it compiles from human-readable code into byte code that is understandable for the JVM or another virtual machine that runs on top of the OS. When executed, the VM interprets this bytecode and converts it into binary code that is understandable to the processor.

JVM-based code execution steps
JVM-based code execution steps

On the other hand, there are C / C ++ that do not use virtual machines, and this allows you to remove one step from the scheme above, while increasing performance. There is a direct compilation of human-readable code into binary.

Stages of code execution in the absence of a bytecode compilation step
But the cleaning and distribution of variables in these languages ​​is still a pain. At the same time, most programming languages ​​process the distribution and deletion of objects using garbage collection and reference counting algorithms.

Go has absorbed the best of both worlds. Like low-level languages, such as C / C ++, Go is a compiled language. This implies a performance close to low-level languages. It also has its own garbage collector for distributing and deleting objects. More malloc () and free () ! Cool!

Go code is easy to maintain.


Let me tell you one thing. Go has no such crazy syntax as in other languages ​​- everything is very neat and clean.

Go developers at Google had this in mind when they created it. Due to the fact that Google has a huge code base and thousands of developers work on it, the code should be easy for other developers to understand, and one section of code should have a minimum of side effects on other areas. This makes the code easier to maintain and make changes.

Go deliberately does not contain many features of OOP languages.


The above changes distinguish Go from other languages ​​and this makes programming on it not similar to programming in other languages. You may not like some of the points above. But this does not mean that you can not write your application without these features. All you have to do is write only 2-3 lines more. But on the other hand, it will make your code more clean and clear.

Graph readability of the code against its performance
Readability of the code against its performance.

The graph above shows that Go is almost as effective as C / C ++, and has the same simple syntax as Ruby, Python, and other languages. This is a win-win option ( win-win. - Prim.perev. ) For both people and for the computer!

Go syntax is very stable, which can't be said about another new Swift language . The syntax has not changed since its public release of version 1.0 in 2012. This gives it backward compatibility.

Go is supported by Google.



Conclusion:


Source: https://habr.com/ru/post/326798/


All Articles