What is the most important feature of go language

青灯夜游
Release: 2022-12-16 19:04:11
Original
5494 people have browsed it

The most important features of the go language are: 1. Concurrent programming; concurrent programming in go syntax is extremely easy, there is no need to deal with callbacks, no need to pay attention to thread switching, only one keyword, simple and natural. 2. Automatic garbage collection; programmers no longer need to consider memory recycling, but the language features provide a garbage collector to reclaim memory. 3. Richer built-in types. 4. Multiple return values ​​of functions. 5. Error handling. 6. Anonymous functions and closures. 7. Types and interfaces. 8. Reflection allows developers to treat the type itself as a first-class value type. 9. Language interactivity.

What is the most important feature of go language

The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.

The most important features of Go language are as follows:

  • Concurrent programming

  • Automatic garbage collection

  • Richer built-in types

  • Multiple return values ​​of functions

  • Error handling

  • Anonymous functions and closures

  • Types and interfaces

  • Reflection

  • Language interactivity

Concurrent programming

Today, concurrency 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.

The Go language is much simpler than most languages ​​​​in terms of concurrent programming. This is one of its biggest highlights and an important bargaining chip for it to enter high-concurrency and high-performance scenarios in the future.

What is the most important feature of go language

Different from traditional multi-process or multi-thread, golang's concurrent execution unit is a coroutine called goroutine.

Because locks are used in shared data scenarios, coupled with GC, its concurrency performance is sometimes not as good as the asynchronous multiplexing IO model. Therefore, compared to most languages, golang's concurrent programming is simpler than concurrency performance. More selling point.

In today’s multi-core era, the significance of concurrent programming is self-evident. Of course, many languages ​​support multi-threaded and multi-process programming, but unfortunately, implementing and controlling it is not so easy and pleasant. The difference with Golang is that the language level supports coroutine (goroutine) concurrency (coroutines are also called micro-threads, which are lighter, less expensive, and higher-performance than threads). It is very simple to operate. The language level provides the keyword (go) Used to start coroutines, and thousands of coroutines can be started on the same machine. Coroutines are often understood as lightweight threads. One thread can contain multiple coroutines, and the shared heap does not share the stack. Coroutines are generally scheduled explicitly by the application, and context switching does not need to go down to the kernel layer, which is much more efficient. There is generally no synchronous communication between coroutines, and there are two types of communication between coroutines in golang: 1) shared memory type, which uses global variable mutex lock to achieve data sharing; 2) message passing type, which uses a unique Channel mechanism for asynchronous communication.

Comparing JAVA's multi-threading and GO's coroutine implementation, it is obviously more direct and simple. This is the charm of GO, solving problems in a simple and efficient way. The keyword go is perhaps the most important symbol of the GO language.

High concurrency is the biggest highlight of Golang language

Memory recycling (GC)

From C to C , from the perspective of program performance, these two languages ​​allow programmers to manage memory by themselves, including memory application and release, etc. Because there is no garbage collection mechanism, C/C runs very fast, but with it comes the programmer's careful consideration of memory usage. Because even a little carelessness may lead to "memory leaks" that waste resources or "wild pointers" that cause program crashes. Although C 11 later used the concept of smart pointers, programmers still need to use it very carefully. Later, in order to improve the speed of program development and the robustness of the program, high-level languages ​​​​such as Java and C# introduced the GC mechanism, that is, programmers no longer need to consider memory recycling, but the language features provide a garbage collector to recycle memory. But what follows may be a decrease in program running efficiency.

The GC process is: first stop the world, scan all objects to determine whether they are alive, mark the recyclable objects in a bitmap area, then immediately start the world, resume services, and at the same time start a special gorountine to reclaim memory. It is placed in the free list for reuse and is not physically released. Physical release is performed periodically by a dedicated thread.

The GC bottleneck is that all objects must be scanned every time to determine liveness. The more objects to be collected, the slower the speed. An empirical value is that it takes 1ms to scan 100,000 objects, so try to use a solution with fewer objects. For example, we consider linked lists, maps, slices, and arrays for storage at the same time. Each element of linked lists and maps is an object, and slice or array is an object, so slice or array is good for GC.

GC performance may be continuously optimized as the version is constantly updated. I have not investigated this carefully. There are HotSpot developers in the team, so they should learn from the design ideas of jvm gc, such as generational recycling, safepoint, etc.

  • Memory is automatically recycled, and developers no longer need to manage memory

  • Developers focus on business implementation, reducing mental burden

  • Only new is required to allocate memory, no need to release it

Richer built-in types

The key is built-in. Common data types such as map and slice are built-in and included by default. There is no need to add them yourself.

Multiple function return values

In C, C, including some other high-level languages, multiple function return values ​​are not supported. But this function is indeed needed, so in C language, the return value is generally defined as a structure, or returned in the form of a function parameter reference. In the Go language, as a new type of language, the goal is to position it as a powerful language and of course cannot give up meeting this demand, so it is necessary to support multiple return values ​​​​from functions.

When defining the function, you can add (a, b, c) after the input parameters, which means there will be 3 return values ​​a, b, c. This feature is available in many languages, such as python.

This syntactic sugar feature has practical significance. For example, we often require the interface to return a triplet (errno, errmsg, data). In most languages ​​that only allow one return value, we can only Put the triple into a map or array and return it. The receiver also needs to write code to check that the return value contains the triple. If multiple return values ​​are allowed, it will be forced directly at the function definition level to make the code More concise and safe.

Language interactivity

Language interactivity refers to whether this language can interact with other languages, such as calling libraries compiled in other languages. .

Most of the C modules are directly reused in the Go language, here called Cgo. Cgo allows developers to mix and write C language code, and then the Cgo tool can extract these mixed C codes and generate C The function's calling wrapper code. Developers can basically completely ignore how the boundary between Go language and C language is crossed.

Golang can interact with C programs, but cannot interact with C. There are two alternatives: 1) First compile c into a dynamic library, and then call a piece of c code from go. The c code dynamically calls the dynamic library through the dlfcn library (remember to export LD_LIBRARY_PATH); 2) Use swig (never played with it)

Exception handling

golang does not support structured exception solutions such as try...catch because it feels that it will increase the amount of code and will be Abuse, throwing exceptions no matter how small. The exception handling method advocated by golang is:

Ordinary exception: the callee returns the error object, and the caller determines the error object.

Serious exception: refers to an interruptive panic (such as division by 0), which is captured and processed using the defer...recover...panic mechanism. Serious exceptions are generally automatically thrown internally by golang, and do not require users to actively throw them, avoiding the situation where traditional try...catch is written everywhere. Of course, users can also use panic('xxxx') to actively throw, but this will degenerate this mechanism into a structured exception mechanism.

Reflection

Go language provides a mechanism to update and check the value of variables, call methods of variables and Variables support intrinsic operations, but the specific types of these variables are not known at compile time. This mechanism is called reflection. Reflection also allows us to treat the type itself as a first-class value type.

Reflection refers to the ability to access and modify the program itself during the running of the program. When the program is compiled, the variables are converted into memory addresses, and the variable names will not be written to the executable part by the compiler. The program cannot obtain its own information.

Languages ​​that support reflection can integrate the reflection information of variables, such as field names, type information, structure information, etc., into the executable file during program compilation, and provide an interface for the program to access the reflection information, so that Reflection information about types can be obtained during program runtime and have the ability to modify them.

The C/C language does not support the reflection function and can only provide very weakened program runtime type information through typeid; languages ​​such as Java and C# all support complete reflection functions; dynamic languages ​​​​like Lua and JavaScript, due to their The syntax itself allows the code to access the program's own value and type information at runtime, so no reflection system is needed.

The reflection system of Go language program cannot obtain all type information in an executable file space or a package. It needs to use the corresponding lexical, syntax parser and abstract syntax tree (AST) in the standard library to analyze the source code. This information is obtained after performing a scan.

【Related recommendations: Go video tutorial, Programming teaching

The above is the detailed content of What is the most important feature of go language. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!