[2] Go language Features at a glance

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

The go language is a static type development language.

The main features of the Go language are as follows:

(1) Automatic garbage collection

(2) richer built-in types

(3) function multiple return value

(4) Error handling

(5) Anonymous functions and closures

(6) Type and interface

(7) Concurrent programming

(8) Reflection

(9) Language interactivity


(a) garbage collection

One problem that may arise for languages that require manual memory management is that if the developer forgets to free up memory, a memory leak issue is raised. If the method is frequently called, the memory will always go crazy and the system memory is all occupied and the program crashes, if the system resources are leaked, it can cause the system to crash. Another problem that can be raised is that the memory block pointed to by the pointer cannot be determined when the pointer is passed around. If the memory is freed somewhere in the code, and other places are using pointers to the memory, then these pointers may become wild or dangling pointers, and any manipulation of these pointers will result in unpredictable consequences.

Because of its outstanding efficiency, C/D + + is a major development language for server systems over a very long period of time. Server crashes are also often caused by incorrect memory and resource management.

In order to solve these problems, a method of memory reference counting has occurred, often referred to as a smart pointer.

The best solution to memory leaks so far is to introduce an automatic garbage collection algorithm at the language level (garbage Collection, referred to as GC), so-called garbage collection, where all memory allocation actions are recorded at run time, and any use of that memory is recorded. The garbage collector then tracks all allocated memory and, once it is discovered that some memory is no longer being used by anyone, periodically recycles the unused memory. of course, because of the need to minimize the performance loss of garbage collection, to reduce the impact on the normal program execution process.

Because C + + itself is too powerful, it has become a difficult task to support garbage collection in C + +. New languages, such as Java and C #, which appear after C + +, have been automatically garbage collected.

Because the go language does not have C + + powerful pointer calculation function, so there is garbage collection function, because of the garbage collection feature support, developers do not have to worry about the object to the problem of failure. We don't have to think about when we need to release previously allocated memory problems, the system will automatically help us determine, and at the right time, such as the CPU is relatively idle when the automatic garbage collection work.

(ii) richer built-in types: Language built-in features allow developers to not bother adding dependent packages.

The go language supports simple built-in types (eg. int, float, etc.), advanced type (array, string, etc.)

(1) The Go language also has a built-in dictionary type (map) for other statically typed languages that are commonly supported by libraries, [why build a map, because most developers need to use this type, each time the import is too cumbersome. ]

(2) array slices (Slice) array slices are an array that can be dynamically grown. Like vectors in the C + + standard library.

(c) function multiple return value

At present, the main language in addition to Phthon basically does not support the function of multiple return value function.

If we want to implement multiple return values, there are generally two approaches, either specifically defining a struct to return (several values) or returning multiple results in the same way as an outgoing parameter.

(iv) Error handling

The Go language introduces 3 keywords for the standard error-handling process, and these three keywords are defer, panic, and recover, respectively. Compared to a capture mechanism in languages such as C + + and Java, the error handling mechanism of the go language can significantly reduce code, without having to add a large number of try-catch statements at a layer level.

(v) anonymous functions and closures

In the go language, all functions are also value types and can be passed as parameters. The Go language supports regular anonymous functions and closures.

(vi) Type and interface

The type definition of the Go language is similar to the struct struct in C, and the struct keyword is used. However, inheritance and overloading are not supported, only the most basic type combination functionality is supported. Similarly, the ability to use complex type systems in C++/java does not show up in the go language.

The go language does not simply subtract from the object-oriented development language, but also introduces the concept of an incredibly powerful "non-intrusive" interface.

(vii) Concurrent programming

The go language introduces the Goroutine concept, making concurrent programming very simple by using goroutine instead of a bare-os concurrency mechanism and by using message passing to share memory instead of using shared memory to communicate.

By using the keyword go before a function call, the function can be executed in a goroutine manner, and Gotoutine is a more lightweight and resource-saving process than threading. The go language dispatches the execution of these functions through the system's threads, so that each function executed with the GO keyword can be run as a unit. When a co-process is blocked, the scheduler automatically arranges other threads to execute the program without waiting for parallelism, and the scheduling overhead is very small, and a CPU schedule is no more than million per second, which allows us to create a large number of goroutine, This makes it easy to write high-concurrency programs to achieve the goal.

The go language implements the CSP (communication sequence process communicating sequential processes) model as the recommended means of communication between Goroutine, in the CSP model, a concurrent system consists of several sequential processes running in parallel, Each process cannot assign a value to another process's variables. Collaboration between processes can only be achieved through a pair of communication primitives. The go language uses the concept of channel to realize the CSP model coincidentally. Channel usage is closer to the concept of piping (pipe) in UNIX systems and facilitates communication across goroutine.

Also, because all goroutine created within a process run in the same memory address space, if different goroutine have to access shared memory variables, the corresponding read-write lock should be acquired before access. The sync pack in the Go Language standard library provides a complete read-write lock function.

(eight) reflection

Reflection is a concept that quickly pops up after the advent of the Java language. With reflection, you can get the details of the object type and manipulate the object dynamically. Reflection is a double-edged sword, powerful but not ideal for readability of code. We do not recommend the use of reflection unless necessary.

The go language reflects most of the functionality of reflection, but does not have a built-in type factory like the Java language, and therefore cannot create an object instance from a type string like java. In Java, you can read the configuration and create a corresponding type based on the type name, which is a common programming technique, but is not recommended in the go language.

The most common usage scenario for reflection is the serialization of objects (serialization). For example, the Encoding/json, Encoding/xml, Encoding/gob, encoding/binary and other packages of the Go Language standard library are heavily dependent on reflection.

(ix) Linguistic interactivity

The go language can reuse existing C modules, and this function is named CGO. CGO is both a language feature and a tool name.

In the Go code, you can write C code in a mix of CGO's attribute syntax, and then the CGO tool can advance the mixed C code and generate a call wrapper code for C functionality. Developers can basically completely ignore how the go language and the C language boundaries are crossed.


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.