Current location - Loan Platform Complete Network - Big data management - Why use Go language? What are the advantages of Go language?
Why use Go language? What are the advantages of Go language?
Many programmers describe Go as a WYSIWYG programming language. In other words, what the code wants to do is exactly the same as the literal meaning. Among these new languages, including D, Go, Rust and Vala, Go once appeared on TIOBE's list. Compared with other new languages, the charm of Go is obviously much greater. Go's mature features will be appreciated by many developers, not just because of its exaggerated exposure. Let's discuss the Go language developed by Google and why Go attracts many developers: compiling Go quickly and simply is very fast, which makes it easy to use as a scripting language. There are several reasons for the high compilation speed: First, Go does not use header files; Secondly, if a module depends on A and A depends on B, then the requirement change in A only needs to recompile the original module and the places that depend on A; Finally, the target module contains enough dependency information, so the compiler does not need to recreate the file. You only need to simply compile the main module, and other parts needed in the project will be compiled automatically. Cool, isn't it? At present, there are two main ways to deal with errors in local languages: directly returning code or throwing exceptions. Neither is the best way to deal with it. The returned code is very frustrating because the returned error code often conflicts with the data returned by the function. Go allows functions to return multiple values to solve this problem. This value returned by the function can be used to check whether the defined type is correct and check the return value of the function anytime and anywhere. If you don't care about the wrong value, you don't have to check it. In both cases, regular return values are available. Simplified components (before inheritance) By using interfaces, types qualify as members of objects, just as Java specifies behavior. For example, in the IO package in the standard library, a Writer is defined to specify a method, a Writer function, and the input parameter is a byte array, which returns an integer value or an error type. The implementation of any type of writer method with the same signature is a complete implementation of IO (Writer Interface). This is decoupling code, not elegance. It also simplifies the simulation object of unit test. For example, if you want to test the methods in a database object, in a standard language, you usually need to create a database object, which requires a lot of initialization and protocols to simulate the object. In Go, if a method needs to implement an interface, you can create any object that is useful for the interface, so you create a MockDatabase, which is a very small object and only implements several interfaces that need to be run and simulated-no constructor, no attachment function, only some methods. Simplify Concurrency Compared with other languages, concurrency in Go is easier. Put the "go" keyword before any function, and the function will automatically run in its go-routine (a very light thread). Go- routines communicate through channels and basically block all queued messages. Common tools are useful for mutual exclusion, but it is easier for Go to use channels to kick out concurrent tasks and coordinates. Excellent error information All languages similar to Go are inferior to Go in their own diagnosis. For example, a deadlock program will inform you which thread caused the deadlock at present when Go runs. The compiled error information is very detailed, comprehensive and useful. There are many other attractions here, so let's briefly introduce them, such as high-order functions, garbage collection, hash mapping and extensible array built-in language (some language syntax, not as a library) and so on. Of course, Go is not perfect. There are still some immature tools and small user groups, but with the continuous development of Google language, there will definitely be rectification measures. Although many languages, especially D, Rust and Vala, aim to simplify C++, they still give people the impression that C++ looks better.

Advantages of Go language

Can be directly compiled into machine code, without relying on other libraries. The version of glibc has certain requirements, and the deployment is completed by throwing a file on it.

Static type language, but it has the feeling of dynamic language. Static typed languages are the most hidden problems that can be detected at compile time. Dynamic languages give people the impression that there are many packages that can be used and the writing efficiency is very high.

Language level supports concurrency, which is the biggest feature of Go. It naturally supports concurrency. I once said that there is a difference between natural genes and plastic surgery. Everyone is equally beautiful, but do you like plastic surgery or natural genetic beauty? Go is a gene-supported concurrency, which can make full use of multi-core and facilitate the use of concurrency.

The built-in runtime supports garbage collection, which is one of the characteristics of dynamic languages. Although GC is not perfect at present, it can cope with most situations we can encounter, especially after Go 1. 1.

Easy to learn, the authors of Go language all have the gene of C, so Go naturally has the gene of C, so there are 25 Go keywords, but their expressive ability is very strong, which almost supports most of the features you have seen in other languages: inheritance, overloading, object and so on.

Rich standard library, Go has built a large number of libraries at present, especially the network library is very powerful, which is also my favorite part.

Built-in powerful tools, Go language has built-in many tool chains, and the best tool should be gofmt tool, which can automatically format the code, which can make the team review so simple, and the code format is exactly the same, so it is difficult to think differently.

Cross-platform compilation, if the Go code you write does not contain cgo, then you can compile linux applications under the window system. How come? Go refers to the code of plan9, which is independent of the system.

Embedded c support, as mentioned earlier, the author is the author of c, so Go can also directly contain c code, using the existing rich c library.