Home > Article > Backend Development > Is go a language?
go is a language. Go (also known as Golang) is a statically strongly typed, compiled, concurrent, and garbage collection programming language developed by Google. Go is a compiled language that uses a compiler to compile code. A compiler compiles source code into a binary (or bytecode) format; as it compiles the code, the compiler checks for errors, optimizes performance, and outputs binaries that can run on different platforms.
The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.
go is a language.
Go (also known as Golang) is a statically strongly typed, compiled language developed by Google's Robert Griesemer, Rob Pike and Ken Thompson. The Go language syntax is similar to C, but its functions include: memory safety, GC (garbage collection), structural form and CSP-style concurrent computing.
The Go language is sometimes described as a "C-like language", or "the C language of the 21st century". Go inherits similar expression syntax, control flow structure, basic data types, call parameter value transfer, pointers and many other ideas from C language. It also has the running efficiency of compiled machine code that C language has always valued and is consistent with existing Seamless adaptation to the operating system.
Because the Go language does not have the concepts of classes and inheritance, it does not look the same as Java or C. But it achieves 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 Go language is a hybrid language.
Go is a compiled language
Go uses a compiler to compile code. A compiler compiles source code into a binary (or bytecode) format; as it compiles the code, the compiler checks for errors, optimizes performance, and outputs binaries that can run on different platforms. To create and run a Go program, a programmer must perform the following steps.
Use a text editor to create a Go program;
Save the file;
Compile the program;
Run the compiled executable file.
This is different from languages like Python, Ruby, and JavaScript, which do not include a compilation step. Go comes with a compiler, so there is no need to install a separate compiler.
Why you should learn Go language
If you want to create system programs or network-based programs, Go language is a very good choice. As a relatively new language, it was designed by experienced and respected computer scientists to address the challenges of creating large-scale concurrent network programs.
Before the emergence of the Go language, developers were always faced with a very difficult choice: whether to use a language (such as: C) that has fast execution speed but unsatisfactory compilation speed, or use a language that has fast compilation speed but not ideal compilation speed. What about languages with poor execution efficiency (such as .NET, Java), or dynamic languages that are less difficult to develop but have average execution speed? Obviously, Go language achieves the best balance between these three conditions: fast compilation, efficient execution, and easy development.
The Go language supports cross-compilation. For example, you can develop applications that can run on Windows on a computer running a Linux system. This is the first programming language to fully support UTF-8. This is not only reflected in the fact that it can process UTF-8 encoded strings, but also its source code file format uses UTF-8 encoding. Go language is truly international!
Features of Go language
Simple syntax
Leave aside the syntax style, just In terms of types and rules, Go has many similarities with C99 and C11, which is also an important reason why the Go language is named "NextC".
The grammar of Go language is at the two extremes of simplicity and complexity. The C language is so simple that every time you write a line of code, you can imagine in your mind what it will look like after compilation, how instructions are executed, how memory is allocated, etc. The complexity of C is that it has too many obscure and irrelevant rules, which is really a headache. In comparison, Go starts from scratch and has no historical baggage. After learning many experiences and lessons, it can plan a world with strict rules and simple organization from scratch.
The grammar rules of Go language are strict, there is no ambiguity, and there are no black magic variations. The code written by anyone is basically the same, which makes the Go language easy to learn. I think it's worth giving up part of the "flexibility" and "freedom" in exchange for better maintainability.
Downgrade " " and "--" from operators to statements, retain pointers, but prevent pointer operations by default, and the benefits are obvious. In addition, using slices and dictionaries as built-in types and optimizing them from the runtime level is also considered "simple".
Concurrency Model
Today, concurrent programming has become a basic skill for programmers, and many related discussion topics can be seen in various technical communities. In this case, the Go language has done something very bold, uncharacteristically, and fundamentally made everything concurrent. It uses Goroutine to run everything during runtime, including the main.main entry function.
It can be said that Goroutine is the most significant feature of Go. It uses a coroutine-like approach to handle concurrent units, but also performs deeper optimization at the runtime level. This makes syntactically concurrent programming extremely easy. There is no need to deal with callbacks, no need to pay attention to thread switching, just one keyword, simple and natural.
With channel, implement the CSP model. Decoupling the data coupling between concurrent units and allowing each to perform its own duties is a welcome relief for all developers struggling with memory sharing and lock granularity. If there are any shortcomings, it is that there should be a larger plan to expand communication from within the process to outside the process to achieve true distribution.
Memory allocation
It is good to make everything concurrent, but it also brings many problems. How to achieve 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 at runtime. Excluding the content modified to cooperate with the garbage collector, the memory allocator completely retains the original structure of tcmalloc. Use cache to provide lock-free allocation for the current execution thread, and multiple centrals to balance memory unit reuse among different threads. At a higher level, the heap manages large blocks of memory and divides them into different levels of reused memory blocks. The fast allocation and secondary memory balancing mechanism allow the memory allocator to excellently complete memory management tasks under high pressure.
In recent versions, compiler optimization has been very effective. 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 except for occasional performance problems that force the use of object pools and autonomous memory management, we basically do not need to participate in memory management operations.
Garbage collection
Garbage collection has always been a problem. In the early years, Java was ridiculed for a long time due to its inefficient garbage collection. Later, Sun successively incorporated many people and technologies to develop to what it is 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, the reclaimed memory cannot be shrunk. Fortunately, pointer arithmetic is blocked, otherwise it would be difficult to achieve accurate recycling.
Every time you upgrade, the garbage collector must be the most modified part of the core component. From concurrent cleanup, to reducing STW time, until version 1.5 of Go implements concurrent marking, and gradually introduces 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 it is still far from being useful.
Static linking
When Go was first released, static linking was promoted as an advantage. Just a compiled executable file can be deployed without attaching anything. This seemed to be a good idea, but then the trend changed. For several consecutive versions, the compiler has been improving the buildmode function of the dynamic library, and the situation has become a bit embarrassing for a while.
Leave aside the unfinished buildmode mode for now, the benefits of static compilation are obvious. The runtime and dependent libraries are directly packaged into the executable file, which simplifies deployment and release operations. There is no need to install the running environment and download many third-party libraries in advance. This simple approach is of great benefit for writing system software, because library dependencies have always been a problem.
Standard Library
The standard library with complete functions and reliable quality provides sufficient power for programming languages. Most basic function development can be completed without the help of third-party extensions, which greatly reduces learning and usage costs. The most important thing is that the standard library is guaranteed to be upgraded and repaired, and it can also obtain the convenience of deep optimization from runtime, which is not available in third-party libraries.
Although the Go standard library is not completely covered, it is still extremely rich. One of the commendable ones is net/http, which can implement a high-performance Web Server with just a few simple statements. This has always been a highlight of publicity. What's more, a large number of excellent third-party frameworks based on this have pushed Go to the position of 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, with a large number of excellent works emerging frequently, which also provides us with a good reference for learning Go.
Toolchain
A complete tool chain is extremely important for daily development. Go has done a pretty good job here, with corresponding tools for compilation, formatting, error checking, help documentation, and third-party package downloads and updates. Its functions may not be perfect, but at least it is simple and easy to use.
Built-in complete testing framework, including unit testing, performance testing, code coverage, data competition, and pprof for tuning. These are essential tools to ensure that the code can run correctly and stably.
In addition, runtime monitoring information can also be output through environment variables, especially garbage collection and concurrent scheduling tracking, which can further help us improve the algorithm and obtain better runtime performance.
For more programming related knowledge, please visit: Programming Video! !
The above is the detailed content of Is go a language?. For more information, please follow other related articles on the PHP Chinese website!