Why do you want to learn the go language? Translation

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Original link Original author: keval Patel

Today in medium saw an article "Why should you learn Go?", I am also in the early stage of learning Golang, I think this article for beginners have great benefits, obtained the consent of the original author, decided to translate it into Chinese. Translator level may not be high, if the article is wrong, please correct me, I am grateful.




"Go to be is the server language of the future." -tobias Lütke, Shopify

In the past few years, there has been a new programming language in which the heat continues to rise:Go or call Golang. the only thing that can make programmers crazy is the new programming language, right? So I started learning the go language four or five months ago and I'm going to tell you why you should also learn the new language.

In this post, I'm not going to teach you how to write "Hello world! "There are many such tutorials on the web. Next I'll explain the current computer hardware-the stage of the software and why we need a new language like go. Perhaps we think that there is no problem, so we do not need to solve this idea, right?

Hardware limitations:

Moore's law is failing.

2004 Intel introduced the first Pentium 4 processor with 3.0GHz speed. Today, my MacBook Pro 2016 already has a 2.9GHz speed. The pace of primary processing has not progressed much in the ten years. In the table below you can see a comparison of the processing power during this period.



As you can see, single-threaded performance and processor frequency remain stable over the last 10 years. If you think that adding more transistors is the solution, then you're wrong. This is because some of the quantum features start to appear (such as tunnels) on a smaller scale, and because it actually invests more transistors (why?). ), and the number of transistors increased per dollar starts to drop.

Therefore, for solutions to the above problems,

    · Manufacturers are starting to join more and more core processors. Now we have four-core and eight-core CPUs available.

    · We have also introduced Hyper-threading technology.

    · Add more cache to the processor to improve performance.

However, the limitations of the above solutions are too large. We cannot increase the performance by adding more and more caches to the processor because the cache has physical limits: the larger the cache, the more full the speed. Adding more cores to the processor also has its costs. Furthermore, it cannot be extended to infinity. These multicore processors can run multiple threads at the same time and bring concurrency. We will discuss it later.

So, if we can't rely on hardware improvements, the only way to improve performance is to have more efficient software. But sadly, modern programming languages are less efficient.

" modern processors are like cars filled with nitro fuel, and it's one-fourth miles they're good at. Unfortunately, modern programming languages are like Monte Carlo, and they are full of twists and turns. "-david Ungar

Go has go!!

As we discussed above, hardware vendors are adding more cores to the processor to improve performance. All of the data centers are running on these processors and we should expect the number of cores to increase over the next few years. Today's applications use multiple microservices to maintain database connections, Message Queuing, and maintenance caches. Therefore, the software and programming languages we develop should support concurrency, and they should support the expansion of the number of cores.

However, most modern programming languages (such as Java,python, etc.) were born in a single-threaded environment in the 90 's, and most of these programming languages support multithreading. But the real problem is concurrent execution, thread locks, race conditions and deadlocks. These problems make it difficult to create multithreaded applications in those languages.

For example, using Java to create a new thread is not efficient in memory. Each thread occupies a memory heap size of about 1MB, and if you turn on thousands of threads, it will put a lot of pressure on the heap and will shut down due to insufficient memory. In addition, this is very difficult if you want to communicate between two or more threads.

On the other hand, the Go language was released in 2009, when multi-core processors are available. That's why go supports concurrency at the language level, and go has a go process instead of a thread. They consume almost 2KB of heap memory. So, you can start the millions of go process at any time.



Other Advantages:

    · The go process has a growing segment stack. This means that they only use more memory when they need it.

    · Go CO turndown thread starts faster.

    · The go process is equipped with the built-in primitives to make communication between them (channels) secure.

    ·  The go process lets you avoid the use of mutexes when sharing data structures.

    ·  In addition, the process and OS threads do not have to map 1 to 1. A single go process can run on multiple threads. The go process can be reused as a few OS threads.

you can get a deeper understanding of the Go association in the speech concurrency of Rob Pike instead of parallel .

The above points allow go to efficiently handle concurrency like java,c and C + +, while performing concurrency like Earlang with straightforward code.



Go takes into account both of these features. Easily write concurrency and efficiently manage concurrency.

Go directly in the bottom run

One of the most notable benefits of using C,c + + instead of other modern high-level languages such as Java/python is their performance. Because C + + + is compiled rather than interpreted as an interpreted type.

Processor understands the binary. Generally, when you build a program that uses Java or other JVM-based languages, when you compile your project, it compiles human-readable code into bytecode that the JVM or other virtual machines running on the underlying operating system can understand. During execution, the virtual machine interprets those bytecode and translates them into binary files that can be understood by the processor.



VM language-based execution steps

On the other hand, C/C + + does not execute on virtual machines and removes this level to improve performance. It compiles human-readable code directly into binary files.


steps to execute based on a binary file

However, releasing and assigning variables in these languages is a huge pain, although most programming languages use garbage collectors or reference counting algorithms to handle the allocation and deallocation of objects.

The go language takes both of these aspects into account. Go is a compiled language, such as low-level languages such as C + + +. This means that performance is almost near the low-level language. It also uses garbage collection to allocate and purge objects. Therefore, there are no more malloc () and free () statements! Cool!!!

Go code is easy to maintain

Let me tell you something. Go does not have the same crazy programming syntax as any other language. Its syntax is very neat and clean.

The Google designers of the go language have thought about this when they created the language. As Google has a very large code base and thousands of developers are working on the same codebase, the code should be simple enough for other developers to understand and one piece of code should have minimal side effects on the code. This makes the code easy to maintain and easy to modify.

Go deliberately omits many of the features of the modern OOP language.

    ·  No class. All things are divided into packages only. At the same time, go uses structs instead of classes.

    · Inheritance is not supported. This makes the code easy to modify. Like the Java/python language, if the ABC class inherits the XYZ class and you change the XYZ class, you might have some side effects in other classes that inherit XYZ. By removing inheritance, we can easily understand the code at the same time (because it is not treated as a superclass when looking at a piece of code ).

    ·  No constructors.

    ·  No annotations.

    ·  There is no generic type.

    ·  There is no exceptions (it should refer to the type of error like Ava).

The above changes make go very different from other languages, and make programming with go language different from other programming languages. You may not like the points above. However, if you do not have the above characteristics, you will not be able to write your program. All you have to do is write more than 2-3 lines. The advantage is that it will make your code clearer.



Readability vs Efficiency of code

The diagram above shows that go is almost as efficient as C/s + +, while code syntax is as simple as Ruby,python and other languages, which is a win for both humans and processors!

Unlike other new Swift-like languages, the go syntax is very stable. Its syntax has remained unchanged since the initial 2012 public release of 1.0, which has enabled it to support backwards compatibility.

Go is supported by Google

    · I know this is not a direct technical advantage. However, it is important that go is designed and supported by Google. Google has one of the world's largest cloud infrastructures, and its scale is expanding. Go is designed by Google to address their support scalability and effectiveness issues. You will face the same problem when creating your own server.

    · More big companies, like Adobe,bbc,ibm,intel and even medium, are using go. (Source: Https://github.com/golang/go/wiki/GoUsers )

Conclusion:

  · Even if go is not used in other object-oriented languages, it is still not negligible. Go provides the same performance as C + +, and ultra-efficient concurrency like Java, with interesting code like Python/perl.

   ·  If you don't have any plans to learn go, I still think that hardware constraints give us pressure, so software developers can write super-efficient code. Developers need to understand the hardware and optimize their programs accordingly. optimized software can run on cheaper and slower hardware, such as running the IOT devices) and the end-user experience will have a greater impact.

If there is a mistake, please correct me ~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.