[Golang] An article takes you to quickly understand the Go language & why you should learn the Go language

Table of contents

1. Why the Internet world needs Go language

1.1 Hardware limitations: Moore's Law has expired 

1.2 Go language is born for concurrency

1.3 Go has powerful performance

1.4 Go language is easy to learn

1.4.1 Concise syntax

1.4.2 Uniform code style

1.4.3 High development efficiency 

2. The birth and development of Go language

2.1 What is the Go language 

 2.2 The birth of Go language

2.3 Go Gopher - the mascot of the Go language

3. Why learn Go language

3.1 Features of Go language

3.1.1 Simple syntax

3.1.2 Concurrency Model

3.1.3 Memory allocation

3.1.4 Garbage Collection

3.1.5 Static linking

3.1.6 Standard library

3.1.7 Toolchain

3.2 Go language is born for concurrency 

3.3 Performance of Go language

4. The prospect of learning Go language 

4.1 Some Go language development projects

4.1.1 Docker

4.1.2 Go language

4.1.3 Kubernetes

4.1.4 etcd

4.1.5 limits

4.1.6 martini

4.1.7 codes

4.1.8 delve

4.2 Some companies using Go language

4.2.1 Google

4.2.2 Facebook

4.2.3 Tencent

4.2.4 Baidu

4.2.5 Qiniu Cloud

4.2.6 JD.com

4.2.7 Xiaomi

4.2.8 360

Author's message:


The Go language is a new language that has been loved by everyone since its inception. But because it is a new language, most programming beginners don't know much about it. In addition, there are not many tutorials and blogs about Go language on the Internet. It is not like C or Java. You can also find a lot of relevant information on the Internet, so there is a long way to go to popularize science and explain Go language knowledge, so this article will lead readers to appreciate the characteristics of Go language, and introduce Go language in domestic and foreign companies. And the application of the project, and at the same time let readers understand the three creators and team members behind this powerful language, and why we should learn Go language! Go language - allows you to write C language code with the development efficiency of writing Python code.

1. Why the Internet world needs Go language

Before answering the title, I would like to ask you a question: There are already too many programming languages ​​in the world, why is there another Go language?

Yes, there are too many languages ​​​​in this world, but why is there another Go language? The establishment and rise of a language is not accidental. If it has nothing to do, then there will be no developers to use it. Therefore, the Go language must have some excellent features that make the Internet world accommodate it.

1.1 Hardware limitations: Moore's Law has expired 

Moore's Law: When the price remains constant, the number of components that can be accommodated on an integrated circuit will double every 18-24 months, and the performance will also double. In other words, the computer performance that each dollar can buy will more than double every 18-24 months. 

As can be seen from the chart above, single-thread performance and processor frequency have remained stable over the past decade. We can't think of adding more transistors as a solution as before, because at smaller scales some quantum properties start to emerge (such as tunneling), and because it is very expensive to put more transistors in the same small space, every The number of transistors that can be added for $1 starts to drop.

Manufacturers began to improve processor performance in other ways:

  1. Adding more and more cores to processors, such as quad-core and octa-core CPUs.
  2. Hyper-threading technology was invented.
  3. Added more cache to the processor to improve performance.

But the above solutions also have their own limitations. Due to cost reasons, we cannot add unlimited cores to the computer, nor can we add unlimited caches to improve performance, because the larger the cache, the lower the performance of the value.

We have no way to keep making breakthroughs in hardware, we need to improve the performance of software or we need high-performance software.

1.2 Go language is born for concurrency

As mentioned above, hardware manufacturers are adding more and more cores to processors to increase performance. All data centers run on these processors, and more importantly, today's applications use multiple microservices to maintain database connections, message queues and maintain caches. Therefore, the software and programming language developed should easily support concurrency and should be able to scale as the number of CPU cores increases.

However, most modern programming languages ​​like Java, Python, etc. come from a single-threaded environment in the 90s. Although some programming language frameworks are constantly improving the efficiency of multi-core resource usage, such as Java's Netty, etc., developers still need to spend a lot of time and energy to understand the operating principles of these frameworks before they can master them.

Go was released in 2009, when multi-core processors were already on the market. The Go language has native design advantages in multi-core concurrency. The Go language natively supports concurrency from the bottom layer, without the need for third-party libraries, developers' programming skills and development experience.

Many companies, especially Internet companies in China, are about to or have completed the process of using the Go language to transform their old systems. The system refactored by Go language can use less hardware resources to achieve higher concurrency and I/O throughput performance. Fully tapping the potential of hardware equipment also satisfies the current market environment of refined operation.

The concurrency of the Go language is based on  threads goroutine , goroutine similar to threads, but not threads. It can be  goroutine understood as a kind of virtual thread. The Go language runtime will participate in scheduling  goroutine, and will be  goroutine reasonably allocated to each CPU to maximize the use of CPU performance. The cost of starting one goroutineis very small (about 2KB of memory), and you can easily create millions of them goroutine.

goroutinespecialty:

  1. goroutineHas a segmented stack that grows. This means they only use more memory when needed.
  2. goroutineStartup time is faster than threads.
  3. goroutineNative support uses channels to communicate securely.
  4. goroutineThere is no need to use mutexes when sharing data structures.

1.3 Go has powerful performance

The biggest benefit of using C, C++ is their performance compared to other modern high-level languages ​​like Java/Python. Because C/C++ is a compiled language rather than an interpreted language. Processors can only understand binary files. High-level languages ​​such as Java and Python need to translate human-readable code into bytecodes when running, and then convert them into binary files that the processor can understand by a special interpreter.

Like C and C++, the Go language is also a compiled language. It directly compiles human-readable code into a binary file that the processor can run directly, with higher execution efficiency and better performance.

It can be seen that the performance of the Go language is closer to the Java language. Although it is not as good as the Java language that has been optimized for many years in some test cases, after all, the Java language has experienced years of accumulation and optimization. Go language will improve single-core performance through continuous version optimization in future versions.

1.4 Go language is easy to learn

1.4.1 Concise syntax

The Go language is easy to learn, with a gentle learning curve, and does not require a two-to-three-year learning period like C/C++ languages. The Go language is called "the C language of the Internet age". The style of the Go language is similar to the C language. Its syntax is greatly simplified on the basis of C language, unnecessary expression brackets are removed, and the loop only has a representation method of for, which can realize various traversals such as numerical values ​​and key values.

1.4.2 Uniform code style

Go language provides a set of formatting tools - go fmt. Some Go language development environments or editors will use formatting tools to format the modified code when saving, so as to ensure that the code submitted by different developers is in a uniform format. (Tucao: No need to worry about those incomprehensible black magic anymore...)

1.4.3 High development efficiency 

 The Go language realizes the perfect combination of development efficiency and execution efficiency, allowing you to write C code (performance) like writing Python code (efficiency).

2. The birth and development of Go language

We just said so much, just want to tell you the advantages of the Go language and its development prospects, so now let's take you to understand the Go language!

2.1 What is the Go language 

The Go language (or Golang) originated in 2007 and was officially released in 2009. Go is a very young language, and its main goal is "combining the development speed of dynamic languages ​​​​such as Python with the performance and safety of compiled languages ​​​​such as C/C++".

The Go language is another attempt at programming language design and a major improvement over C-like languages. It not only allows you to access the underlying operating system, but also provides powerful support for network programming and concurrent programming. The Go language has many uses, and can be used for network programming, system programming, concurrent programming, and distributed programming.



The introduction of the Go language aims to reduce the complexity of the code without losing the performance of the application. It has the advantages of "simple deployment, good concurrency, good language design, and good execution performance". At present, many IT companies in China have adopted Go. Language development projects.

The Go language is sometimes described as a "C-like language", or "the C of the 21st century". Go inherits many ideas from C language, such as expression syntax, control flow structure, basic data type, call parameter passing value, pointer, etc., as well as the operating efficiency of compiled machine code and the existing Seamless adaptation of the operating system.

Because the Go language has no concepts of classes and inheritance, it doesn't look the same as Java or C++. But it implements polymorphism through the concept of interface. The Go language has a clear and easy-to-understand lightweight type system, and there is no hierarchy between types. Therefore, it can be said that the Go language is a hybrid language.

In addition, many important open source projects are developed in Go language, including Docker, Go-Ethereum, Thrraform and Kubernetes. 

 2.2 The birth of Go language

Go language has been used as a 20% part-time project of Google since September 21, 2009, that is, relevant employees use 20% of their free time to participate in the research and development of Go language.

Google's "20% time" working method allows engineers to spend 20% of their time on research projects they like. The voice service Google Now, Google News, Google News, and traffic information on Google Maps are all products of 20% of the time.

The three leaders of the project are all well-known IT engineers:

1) Ken Thompson

A member of the Unix team at Bell Labs, one of the founders of C language, Unix and Plan 9, in the 1970s, designed and implemented the original UNIX operating system. From this point of view, how can his contribution to computer science be emphasized? Not too much. He also collaborated with Rob Pike on the design of the UTF-8 encoding scheme.

2) Rob Pike

The general director of the Go language project and a member of the Unix team of Bell Labs. In addition to helping design UTF-8, he also helped develop the distributed multi-user operating system Plan 9, the Inferno operating system and the Limbo programming language, and co-authored " The Unix Programming Environment", made an orthodox elaboration on the design concept of UNIX.

3) Robert Griesemer

Worked at Google, participated in the development of the Java HotSpot virtual machine, has a deep understanding of language design, and is responsible for the code generation part of Google's V8 JavaScript engine used by Chrome browser and Node.js.

These heavyweights in the field of computer science designed the Go language to meet the needs of Google. It took two years to design the language, incorporating the entire team's years of experience and deep knowledge of programming language design. The design team drew on the design wisdom of Pascal, Oberon, and C, while giving Go the convenience of a dynamic language. Therefore, the Go language embodies the language design philosophy of experienced computer scientists and is designed for one of the world's largest Internet companies.

All the designers of the Go language say that they designed the language because C++ brought them frustration. In the Go Design Team meetup at Google I/O 2012, Rob Pike had this to say:

We do a lot of C++ development and get tired of waiting for compilation to finish, and while this is a joke, it's largely true.

2.3 Go Gopher - the mascot of the Go language

The Go language has a mascot, and conferences, documentation pages, and blog posts mostly include the Go Gopher pictured below, designed by talented illustrator Renee French, who is also the wife of one of Go's designers, Rob Pike.

3. Why learn Go language

3.1 Features of Go language

The Go language, also known as Golang, is a statically strongly typed, compiled, concurrent, and garbage-collected programming language developed by Google.



Next, we will introduce the characteristics of Go language in detail from several aspects.

3.1.1 Simple syntax

Leaving aside the grammatical style, in terms of types and rules alone, Go has many similarities with C99 and C11, which is also an important reason why the Go language is named "NextC".

The syntax of the Go language is at the extremes of simplicity and complexity. The C language is so simple that every time you write a line of code, you can imagine the compiled appearance, how instructions are executed, how memory is allocated, and so on. The complexity of C is that it has too many obscure and marginal rules, which is really a headache. In comparison, Go starts from scratch and has no historical burden. After learning many lessons, it can plan a world with strict rules and simple order from scratch.

The grammatical rules of the Go language are strict, there is no ambiguity, and there is no black magic variation usage. The code written by anyone is basically the same, which makes the Go language easy to learn. I think it is worthwhile to give up some "flexibility" and "freedom" in exchange for better maintainability.

The benefits of downgrading "++" and "--" from operators to statements, retaining pointers, but preventing pointer arithmetic by default are obvious. Also, using slices and dictionaries as built-in types and optimizing them at runtime is considered "simple".

3.1.2 Concurrency Model

Today, concurrent programming has become a basic skill for programmers, and many discussion topics related to it can be seen in various technical communities. In this case, the Go language has done a very bold thing uncharacteristically, fundamentally making everything concurrent, and using Goroutine to run everything at runtime, including the main.main entry function.

Goroutines are arguably the most distinguishing feature of Go. It uses a coroutine-like way to handle concurrent units, but it does a deeper optimization at the runtime level. This makes syntactically concurrent programming extremely easy, no need to deal with callbacks, no need to pay attention to thread switching, only one keyword, simple and natural.

With the channel, realize the CSP model. Dismantling the data coupling between concurrent units and performing their duties is a relief that all developers who struggle with memory sharing and lock granularity can expect. If there are deficiencies, it is that there should be a bigger plan to expand communication from within the process to outside the process to achieve true distribution.

3.1.3 Memory allocation

It is good to make everything concurrent, but it also brings many problems. How to realize memory allocation and management under high concurrency is a difficult problem. Fortunately, Go chose tcmalloc, which is a high-performance memory allocation component designed for concurrency.

It can be said that the memory allocator is the least changed part of the three major components of the runtime. The memory allocator completely retains the original architecture of tcmalloc, excluding the modified content due to the cooperation with the garbage collector. Use cache to provide lock-free allocation for the current execution thread, and multiple centrals balance memory unit reuse among different threads. At a higher level, the heap manages a large block of memory to be divided into multiplexed memory blocks of different levels. The fast allocation and secondary memory balance mechanism enable the memory allocator to perform memory management tasks under high pressure.

Compiler optimizations have been very effective in the last few releases. It will try its best to allocate objects on the stack to reduce garbage collection pressure, reduce management consumption, and improve execution performance. It can be said that we basically do not need to participate in memory management operations, except for occasional performance issues where we are forced to use object pools and autonomous memory management.

3.1.4 Garbage Collection

Garbage collection has always been a problem. In the early years, Java was ridiculed for a long time due to the inefficiency of garbage collection. Later, Sun continuously absorbed a lot of people and technology to develop to today. But even so, in large-memory application scenarios such as Hadoop, garbage collection is still stretched and difficult.

Compared to Java, Go faces more difficulties. Due to the existence of pointers, reclaimed memory cannot be shrunk. Fortunately, pointer arithmetic is blocked, otherwise it would be difficult to achieve precise recovery.

Every time you upgrade, the garbage collector must be the most modified part of the core components. From concurrent cleaning, to reducing STW time, until Go version 1.5 implements concurrent marking, gradually introducing three-color marking and write barriers, etc., all are to make garbage collection work better without affecting user logic. Despite the efforts, the current version of the garbage collection algorithm can only be said to be usable, and there is still a lot of distance from being easy to use.

3.1.5 Static linking

When Go was first released, static linking was advertised as a virtue. It only needs to be compiled as an executable file, and it can be deployed without adding anything. This seemed to be a good thing, but then the mood changed. After several versions, the compiler is improving the buildmode function of the dynamic library, and the scene becomes awkward for a while.

Leaving aside the unfinished buildmode mode, the benefits of static compilation are obvious. The runtime and dependent libraries are directly packaged into the executable file, which simplifies the deployment and release operations, without the need to install the runtime environment and download many third-party libraries in advance. This simplicity is of great benefit when writing system software, since library dependencies have always been a hassle.

3.1.6 Standard library

Full-featured, reliable standard libraries provide plenty of power for programming languages. Most of the basic function development can be completed without the help of third-party extensions, which greatly reduces the cost of learning and use. The most important thing is that the standard library has upgrade and repair guarantees, and can also obtain the convenience of deep optimization from runtime, which is not available in third-party libraries.

Although the Go standard library cannot be said to be fully covered, it is also extremely rich. Among them, net/http is commendable, which can realize a high-performance Web Server with only a few simple statements, which has always been the highlight of publicity. What's more, a large number of excellent third-party Frameworks based on this have pushed Go to one of the Web/Microservice development standards.

Of course, excellent third-party resources are also an important part of the language ecosystem. Among the several languages ​​that have emerged in recent years, Go is unique, and a large number of excellent works emerge frequently, which also provides a good reference for us to learn Go.

3.1.7 Toolchain

A complete toolchain is extremely important for day-to-day development. Go does a pretty good job here, whether it is compilation, formatting, error checking, help documentation, or third-party package download and update, there are corresponding tools. Its functions may not be perfect, but at least it is easy to use.

A complete built-in testing framework, including unit testing, performance testing, code coverage, data competition, and pprof for tuning, is an essential tool to ensure that the code can run correctly and stably.

In addition, runtime monitoring information can be output through environment variables, especially garbage collection and concurrent scheduling tracking, which can further help us improve the algorithm and achieve better runtime performance.

3.2 Go language is born for concurrency 

In the early days, CPUs executed machine instructions sequentially in the form of a single core. The C language, the ancestor of the Go language, is the representative of this sequential programming language. The sequence in a sequential programming language means that all instructions are executed in a serial manner, and at the same time, there is only one CPU executing the instructions of the program in sequence.

With the development of processor technology, the single-core era has encountered bottlenecks in the way of increasing processor frequency to improve operating efficiency. The stagnation of single-core CPU development has brought opportunities for the development of multi-core CPUs. Correspondingly, programming languages ​​have also begun to gradually develop in the direction of parallelization.

Although some programming language frameworks are constantly improving the efficiency of multi-core resource usage, such as Java's Netty, etc., developers still need to spend a lot of time and energy to understand the operating principles of these frameworks before they can master them.

As a programmer, it is very difficult to develop applications that can make full use of hardware resources. Modern computers have multiple cores, but most programming languages ​​do not have effective tools for programs to easily use these resources. When programming, you need to write a lot of thread synchronization code to take advantage of multiple cores, which can easily lead to errors.

The Go language is a programming language that natively supports concurrency born in the context of multi-core and networking. The Go language natively supports concurrency from the bottom layer, without the need for third-party libraries, and developers can easily decide how to use CPU resources when writing programs.

The concurrency of Go language is based on goroutine. Goroutine is similar to thread, but not thread. A goroutine can be understood as a virtual thread. The Go language runtime will participate in scheduling goroutines, and allocate goroutines to each CPU reasonably to maximize CPU performance.

Among multiple goroutines, the Go language uses channels to communicate. A channel is a built-in data structure that allows users to send typed messages synchronously between different goroutines. This makes the programming model more geared toward sending messages between goroutines rather than multiple goroutines competing for access to the same data.

The program can design the link that requires concurrency as a producer mode and a consumer mode, and put data into the channel. The code at the other end of the channel performs concurrent calculations on the data and returns the results, as shown in the figure below.

3.3 Performance of Go language

For the execution speed of Go, you can refer to a language performance test data website: Which programming language is fastest? (Benchmarks Game) (pages.debian.net)

  • Go's performance is close to C++
  • The algorithm time cost is close to that of Java, but the memory cost is much lower than that of Java
  • Compared with python, the time and memory overhead are excellent

reason:

  • Use importcitation management
  • No template compilation burden
  • Compiler optimization after version 1.5
  • The language itself has few keywords

4. The prospect of learning Go language 

At present, the Go language has been widely used in artificial intelligence, cloud computing development, container virtualization, big data development, data analysis and scientific computing, operation and maintenance development, crawler development, game development and other fields.

The Go language is easy to learn and naturally supports concurrency, which perfectly fits the current high-concurrency Internet ecology. The job demand of the Go language continues to rise. The current number of Go programmers is small and the salary is good.

Grasp the trend and learn to be a leader rather than a follower.

The domestic demand for Go language has huge potential. At present, whether it is a large domestic manufacturer or an emerging Internet company, basically there will be job demand for Go language.

4.1 Some Go language development projects

All programming languages ​​reflect a rethinking of the language designers' programming philosophy, often including improvements where previous languages ​​exposed some deficiencies. Since the release of version 1.0, the Go language has attracted the attention of many developers and has been widely used. The simplicity, efficiency, and concurrent features of the Go language have attracted many traditional language developers to join, and the number is increasing.

There are many open source projects developed in Go language. The early Go language open source projects were only implemented by binding C language libraries with traditional projects through Go language, such as Qt, Sqlite, etc.; many later projects used Go language for re-native implementation. This process is simpler than other languages. This has also contributed to the emergence of a large number of native development projects using the Go language.

Listed below are some of the projects developed natively using the Go language.

4.1.1 Docker

Docker is an operating system-level virtualization technology that can isolate the operating system and applications, also known as containers. Docker can quickly run one or more instances on a physical server. For example, start a CentOS operating system and end after executing instructions on its internal command line, the whole process is as efficient as if you are in the operating system.


项目链接:GitHub - moby/moby: Moby Project - a collaborative project for the container ecosystem to assemble container-based systems

4.1.2 Go language

The early source code of Go language itself was written in C language and assembly language. From Go 1.5 version, it is completely written in Go language itself. The source code of the Go language is of great reference significance for understanding the underlying scheduling of the Go language. It is recommended that readers who want to have an in-depth understanding of the Go language read it.

Project link: GitHub - golang/go: The Go programming language

4.1.3 Kubernetes

A container scheduling service built on top of Docker developed by Google, users can manage cloud container clusters through Kubernetes clusters. The system will automatically select appropriate working nodes to perform specific container cluster scheduling and processing tasks. Its core concept is Container Pod (container warehouse).

Project link: GitHub - kubernetes/kubernetes: Production-Grade Container Scheduling and Management

4.1.4 etcd

A distributed and reliable KV storage system that can be quickly configured in the cloud. Developed and maintained by CoreOS, the key-value storage system is written in the Go language and handles log replication through the Raft consensus algorithm to ensure strong consistency.

项目链接:GitHub - etcd-io/etcd: Distributed reliable key-value store for the most critical data of a distributed system

4.1.5 limits

beego is a Tornado framework similar to  Python  . It adopts the design idea of ​​RESTFul and uses Go language to write an extremely lightweight, highly scalable and high-performance web application framework.

项目链接:GitHub - astaxie/beego: beego is an open-source, high-performance web framework for the Go programming language.

4.1.6 martini

A Go language framework for quickly building modular web applications.

Project link: GitHub - go-martini/martini: Classy web framework for Go

4.1.7 codes

Excellent domestic distributed  Redis  solution. Codis can be understood as Nginx in the field of Web services, which implements reverse proxy and load balancing for Redis.

Project link: GitHub - CodisLabs/codis: Proxy based Redis cluster solution supporting pipeline and scaling dynamically

4.1.8 delve

A powerful debugger for the Go language, integrated by many integrated environments and editors.

Project link: GitHub - derekparker/delve: Delve is a debugger for the Go programming language.

4.2 Some companies using Go language

The Go language is a programming language released by Google in 2009. Since its launch, it has quickly become popular all over the world with its high development efficiency and perfect running speed. It is known as the "C language of the 21st century".

Now more and more companies are starting to use the Go language to develop their own services, and many services and applications using the Go language have also been born. Let's take a look at which big companies are using the Go language.

4.2.1 Google

As the Google company that created the Go language, of course it will support the Go language. Google has many open source projects developed based on Go, such as kubernets and docker.

4.2.2 Facebook

Facebook is also using the Go language, for which they also established an open source organization facebookgo on Github. You can   visit Facebook's open source projects through Meta Go GitHub , the most representative of which is the famous smooth restart tool grace.

4.2.3 Tencent

In 15 years, Tencent has already practiced Docker on a scale of 10,000 units. Because Tencent's main development language is C/C++, it is much more convenient and has many advantages in using Go language. However, the accumulated C/C++ code is difficult to modify and dare not move, so it mainly tries to use Go in new business. .

4.2.4 Baidu

Baidu mainly uses Go language in operation and maintenance. For example, a BFE project of Baidu operation and maintenance is mainly responsible for the access of front-end traffic. Secondly, the server side of Baidu messaging system also uses Go language.

4.2.5 Qiniu Cloud

Qiniuyun is the first company in China to choose the Go language as the server. As early as 2011, when the grammar of the Go language was not fully stabilized, Qiniuyun had already chosen Go as the main language of the storage server.

4.2.6 JD.com

The JD Cloud message push system, cloud storage, and the list page of JD Mall are all developed in Go language.

4.2.7 Xiaomi

Xiaomi's support for the Go language lies in the open source of the operation and maintenance monitoring system. Its official website is  Open-Falcon - Monitoring system & time series database . In addition, teams such as Xiaomi Interactive Entertainment, Xiaomi Mall, Xiaomi Video, and Xiaomi Ecosystem are all using the Go language.

4.2.8 360

360 also uses the Go language a lot, such as the open source log search system Poseidon, you can check it through  GitHub - Qihoo360/poseidon: A search engine which can hold 100 trillion lines of log data.  There is also 360’s push team Use the Go language.



In addition to the above mentioned, many companies have begun to try to use the Go language, such as Meituan, Didi, Sina, etc.

The strength of the Go language is that it is suitable for developing network concurrency services, such as message push, monitoring, containers, etc., so most companies will give priority to Golang as the development language for high-concurrency projects.

Author's message:

Looking back, the light boat has passed the Ten Thousand Mountains. Looking forward, the long road is long and bright.  Opportunities are always reserved for those who are prepared. We should not blindly follow the trend, but be a leader. Come on, learn Golang from now on, believe it Your future self will definitely thank you for what you did back then!

I will always love my friends who like me to support me and to fight with me in this endeavor of this industry. Hope you have a bright future forever!

Guess you like

Origin blog.csdn.net/qq_62464995/article/details/130277359