- Why should you learn Go?
- By Keval Patel
- The Nuggets translation Project
- Permanent link to this article: github.com/xitu/gold-m…
- Translator: todaycoder001
- Proofread by: TokenJan, JackEggie
Why do you want to learn Go?
“Go will be the server language of the future.” — Tobias Lutke, Shopify
In the past few years, a new language has emerged: Go or GoLang. Nothing excites developers more than a new programming language, right? So I started learning Go four or five months ago. Here I’ll tell you why you should learn the new language, too.
In this article, I’m not going to teach you how to write “Hello World!!” . There are many other articles on the web that will teach you. ** I will explain the current state of hardware and software development and why we need to learn a new language like Go. ** Because if there weren’t any problems, we wouldn’t need a solution, would we?
Hardware limitations
Moore’s lawFailing.
Intel introduced the first Pentium 4 processor with a 3.0 GHz clock speed in 2004. Today, my 2016 MacBook Pro clocks at 2.9 GHz. As a result, raw processing power has not increased much for almost a decade. You can see the increase in processing power relative to time in the figure below.
As you can see from the chart above, single-threaded performance and processor frequency have remained nearly constant over the last decade. If you think adding more transistors is the answer, you are wrong. That’s because at the microscopic scale, quantum properties start to emerge (e.g., quantum tunneling), and more transistors cost more (why?). And the number of transistors you can add per dollar is starting to fall.
Therefore, the solutions to the above problems are as follows:
- Vendors started adding more and more cores to processors. Today, we have four – and eight-core cpus available.
- We also introduced hyper-threading technology.
- Add more cache to the processor to improve performance.
However, these schemes have their own limits. We cannot add more caches to the processor to improve performance because caches have physical limitations: the larger the cache, the slower it is. Adding more cores to the processor also has its costs. And it doesn’t scale indefinitely. These multicore processors can run multiple threads at the same time, and they also bring concurrency capabilities. We’ll talk about that later.
Therefore, if we cannot rely on hardware improvements, the only way out is to find an efficient software to improve performance, but unfortunately, modern programming languages are not that efficient.
“Modern processors are like a straight-line racing car with a nitrogen-oxygen acceleration system, and they do very well in straight-line racing. Unfortunately, modern programming languages are like Monte Carlo racetracks, with lots of corners.” – David Ungar
Go has Goroutine!!
As mentioned above, hardware vendors are adding more cores to processors to improve performance. With all data centers running on these processors, we should expect the number of cores to grow in the coming years. More importantly, today’s applications use multiple microservices to maintain database connections, message queues, and caches. Therefore, we develop software and programming languages that can more easily support concurrency, and they should be extensible as the number of cores grows.
But most modern programming languages (Java, Python, etc.) come from the single-threaded environment of the 1990s. Most of these languages support multithreading. But the real problem is concurrent execution, thread locks, race conditions, and deadlocks. These issues make it difficult to create a multithreaded application in these languages.
For example, creating a new thread in Java consumes a lot of memory. Because each thread consumes about 1 MB of heap memory, if you run thousands of threads, they will strain the heap so much that they will eventually crash due to lack of memory. In addition, it is very difficult for you to communicate between two or more threads.
Go, on the other hand, was released in 2009, when multicore processors were already on the market. This is why Go is built with concurrency in mind. Go replaces threads with Goroutines, which consume about 2 KB of memory from the heap. So you can launch millions of Goroutines at any time.
Other benefits:
- Goroutine has a growable segmented stack, which means it uses more memory only when it needs it.
- Goroutine starts faster than threads.
- Goroutine comes with built-in primitives that allow secure communication between them.
- Goroutine allows you to avoid using mutex when sharing data structures.
- In addition, there is no 1:1 mapping between Goroutine and system threads. A single Goroutine can run on multiple threads. Goroutine can also be reused on a small number of system threads.
You can gain insight from Rob Pike’s excellent lecture concurrency not parallelism.
These points enable Go to be as concurrent as Java, C, or C++, but as elegant as Erlang without the rigor of concurrent execution.
Go runs directly on the underlying hardware
The biggest advantage of using C and C++ over other modern high-level languages (such as Java/Python) is its performance, since C/C++ is a compiled language rather than an interpreted language.
The processor understands binary files. Generally speaking, when you compile an application built in Java or any other JVM-based language, it compiles human-readable code into byte code that can be understood by the JVM or other virtual machines running on top of the underlying operating system. When executed, the virtual machine interprets the bytecodes and converts them into binary files that the processor can understand.
On the other hand, C/C++ does not execute on the VM, and removing the step (compiled to bytecode) from the execution cycle improves performance. It compiles human-readable code directly into binaries.
However, releasing and assigning variables in these languages is extremely painful. Most programming languages use garbage collectors or reference-counting algorithms to handle object allocation and removal.
Go has the best of both worlds. It is a compiled language like some lower-level languages (such as C/C++), which means its performance is close to that of lower-level languages, and it also uses garbage collection to allocate and delete objects. Therefore, malloc() and free() declarations are no longer needed!! This is so cool!!
Code written in Go is easy to maintain
I’ll tell you one thing, Go is not as crazy about programming syntax as other languages, it’s very clean.
The designers of Go had this in mind when Google created the language. Since Google has a very powerful code base, thousands of developers are working on the same code base, the code should be easy for other developers to understand, and one piece of code should have minimal impact on another. All of this makes the code easy to maintain and easy to change.
Go intentionally ignores some features of many modern object-oriented languages.
- There is no class. All code is separated by package only, and Go only has structures, not classes.
- Inheritance is not supported. This will make the code easy to modify. In other languages, such as Java/Python, if class ABC inherits from class XYZ and you make changes in class XYZ, this may cause side effects in other classes that inherit from class XYZ. By removing inheritance, Go also makes it easy to understand code (because you don’t have to look at the parent class at the same time when you’re looking at a piece of code).
- There is no constructor.
- No notes.
- There are no generics.
- There is no exception.
These changes make Go very different from other languages, which makes programming in Go very different from other languages. You may not like some of the ideas above. That’s not to say, however, that you can’t code your application without these features. All you have to do is write a few more lines of code, but on the positive side, it will make your code clearer and add more clarity to your code.
As you can see above, Go is almost as efficient as C/C++, while keeping code syntax concise like Ruby, Python, and some other languages, a win-win situation for both the human and the processor!!
Unlike new languages like Swift, Go has a very stable syntax. It has remained unchanged and backward compatible since the first public release of version 1.0 in 2012.
Go is endorsed by Google
- I know it’s not a direct technical advantage, but Go is designed and supported by Google, which has one of the largest cloud infrastructures in the world, and it’s massive. Google designed Go to address scalability and effectiveness issues. These are the kinds of problems we encounter when creating our own server.
- Go is also used by some big companies, such as Adobe, BBC, IBM, Intel and even Medium. (Source: github.com/golang/go/w…)
conclusion
- Although Go is very different from other object-oriented languages, it has also had a huge impact. Go offers high performance like C/C++, efficient concurrency like Java, and coding fun like Python/Perl.
- If you don’t have any plans to learn Go, I would still say that hardware constraints put pressure on us, and software developers should write super-efficient code. Developers should understand the hardware and optimize their applications accordingly. Optimized software can run on cheaper or slower machines (such as iot devices) and have a better overall impact on the end-user experience.
~ If you like this article, click 💚 below so that more people can see it! In addition, you can also use theMediumorMy blogFollow me for updates on Go!! ~
reference
- The future of GoLang, or developers, comes from Edoardo Paolo Scalafiotti
- Write the next generation server in Go
- Concurrency is not parallelism by Rob Pike
- Why Go?
If you find any mistakes in your translation or other areas that need to be improved, you are welcome to the Nuggets Translation Program to revise and PR your translation, and you can also get the corresponding reward points. The permanent link to this article at the beginning of this article is the MarkDown link to this article on GitHub.
The Nuggets Translation Project is a community that translates quality Internet technical articles from English sharing articles on nuggets. The content covers Android, iOS, front-end, back-end, blockchain, products, design, artificial intelligence and other fields. If you want to see more high-quality translation, please continue to pay attention to the Translation plan of Digging Gold, the official Weibo, Zhihu column.