Go language Learning--born in a noble family

Source: Internet
Author: User

Undoubtedly, in the field of programming languages, go is becoming a rising star. Because of its excellent performance and development efficiency, is being adopted by more and more companies, foreign big companies such as Google, Facebook and so on do not say, the domestic headlines, drops, millet, seven qiniu are actively trying.

Go is hot not only in traditional Internet applications, but also in the emerging blockchain sector, such as the Ethereum Smart Contract framework and the business Blockchain framework, which uses the go language.

Today we will come together to meet the rising star of the programming language industry.

I. Birth

The go language was born in Google and its authors are all God-like characters:

    • Ken Thompson (Ken Thompson,http://en.wikipedia.org/wiki/ken_thompson): designed the B and C languages, created the UNIX and Plan 9 operating system, the 1983 Turing Award winner, the co-author of the Go language.
    • Rob Paik (Rob pike,http://en.wikipedia.org/wiki/rob_pike): Member of the UNIX team, participates in PLAN9 and Inferno operating systems, participates in limbo and go language development, UNIX programming environment One of the authors.
    • Robert Griesemer: Robert Grizemer, who helped make Java's hotspot compiler and Chrome browser's JavaScript engine V8.
    • Brad Fitz Patrick (Brad Fitzpatrick,http://en.wikipedia.org/wiki/brad_fitzpatrick): LiveJournal's founder, author of the famous Open source project memcached.

And most of them came from Bell Labs, which had produced countless computer science and technology results, and later joined Google. The Plan9 described above are their previous projects in Bell Labs, and the Limbo language is derived from the Plan9 project, which then became the predecessor of the Go language.

Time went back to 2007, when the Go language was a lab project in which the big guys were entertaining Google in their spare time. Many of them are the C language authors, C language although in many low-level implementations still have irreplaceable role, but C language is born in the 70 's, in 21st century, the Internet flourished today, C language is not suitable for Internet applications. As a result, they have developed the idea of developing a C language in the Internet age.

In 2009, Google released the first informal version of the Go language, which was released in 2012, and has been upgraded to 1.10 in May 2018.2 version.

It is no exaggeration to say that if the 11-year-old go language was born from 2007 to today is a high-spirited teenager, he has a noble pedigree, with C language and many modern language of fine genes, from the beginning of the high-profile, now is striding forward!

Two. Design ideas

The goal of the go language is to provide a programming language for the internet era, so the following problems (as opposed to the C language) are solved when compared to traditional standalone applications:

    • Concurrency: Although the C language can also provide concurrency support, but the use of high cost, today but many network applications are high concurrency has higher requirements, and the number of server cores more and more

    • Large-scale collaboration: C language is a process-oriented language, in the development of large-scale application is not satisfactory, rely on management is a headache

    • Programming efficiency: The advantage of C is the execution efficiency, but the development efficiency is very low, the C + + and Java and other object-oriented language also appear too cumbersome

    • Garbage collection: C language needs to manage its own memory, which is a very high threshold of technical work, many high-level languages such as Java has long provided a complete solution of garbage collection

The goals the go language expects to achieve are:

    • Native supports multi-core compute core concurrency

    • Simple and efficient dependency management, fast compilation and build

    • Improve development efficiency

    • Provides programmer-friendly features such as garbage collection

While achieving these goals, the go language has an obvious design principle: it does not provide features that are not particularly necessary to cause problems, and it provides an easy-to-use implementation of the important features that are often used. In other words: The new features of the addition of extreme restraint, for the implementation of common functions to pursue the ultimate!

Here are a few examples:

    • The go language does not provide method overloading: The method overloads seem to give programmers a better way to name and manage methods, avoid excessive method names, but also add additional overhead to compiling and linking, rather than providing method overloading, and the Go language kills this feature.

    • A keyword to start a new thread: Go XXX

    • Forcing code writing specifications, such as locations, can only be written in such a way that the { code specification is largely ensured and readability is improved

In the go official documentation about why the go language does not provide such a feature, there is such an interesting explanation, perhaps can intuitively feel the design idea of the Go language:

Every language contains novel features and omits someone ' s favorite feature. Go is designed with a eye in felicity of programming, speed of compilation, orthogonality of concepts, and the need to s Upport features such as concurrency and garbage collection. Your favorite feature May is missing because it doesn ' t fit, because it affects compilation speed or clarity of design, or Because it would make the fundamental system model too difficult.

If It bothers you this go is missing feature X, please forgive us and investigate the features that go does has. You might find this they compensate in interesting ways for the lack of X.

Three. Core Features

Needless to say, the dry goods directly:

Automatic garbage collection

C and C + + flexible pointers and memory control allows programmers to freely sway, feel free and unrestrained, as if the world is in control, in the program world, you are God, master the oppressing of all objects! But this often also means a security breach, with the claws of the memory leak ready to reach our fragile program.

Java, such as the provision of garbage collection characteristics of the language can be said to completely liberate the programmer, from the programmer to write code like a five-star hotel, want to eat what directly take is, eat a pat on the bottom of the meal, and those who wipe the table to wash the dishes to the waiter.

The go language, with its lofty ideals, will naturally provide such advanced services to programmers.

function multiple return value

Most programming language functions only support the return of a value, and if there is a case where multiple values are returned, there are usually two ways:

    1. Returns an object

    2. Pass a pointer to the return value in the function's arguments and assign a value in the function

Which of these methods is not really elegant, and the go language directly supports the return of multiple values, like this:

ip, port := getServerInfo()

Anonymous functions and closures

Anonymous functions and closures are features of dynamic languages such as javascript,php, which provide more flexible expression and improve development efficiency in certain scenarios. Go also provides a good support for both.

Non-hierarchical type system

We all know that class inheritance is supported in object-oriented languages such as C + + and Java, which forms a relational tree of classes, as shown in:

In the go language, display inheritance is not supported, that is, there are no keywords in Java extends implement . But this does not mean that the go language can not achieve object-oriented inheritance, just say go for a more flexible way, that is, non-intrusive inheritance, simply because there is no obvious class hierarchy, the same implementation of the above functions, only the following declaration can be:

As long as dog and cat implement the methods in the animal class, go considers them to be related, so you can do the following:

type Animal interface {    eat()}type Cat struct {}func (c *Cat) eat() {    fmt.Println("cat eat")}func main() {    var animal Animal = new(Cat)    animal.eat()}

The main advantages of this are the following points:

    • Interfaces are loosely related to classes, not strongly bound, and as long as a class implements all the methods of the class interface, this class actually implements the interface, which provides some flexibility
    • Make the programmer more concerned with the business implementation, not spend too much time on the interface design, even can first implement the business class, and then declare a suitable interface as needed

Concurrent

The go language introduces a concept called goroutine , or a co-process, which is a more lightweight concurrency unit than threads, and only needs to use the Go keyword line of code to start the process. As a result, the go language is excellent for high concurrency processing. There is a comparison of concurrency performance from abroad, although may not be able to reflect the full power of go, but still can be seen to some extent the concurrency performance advantage of Go: Each thread performs a hash operation, 5000 concurrent in different languages of the QPS comparison is as follows:

Performance

Go is a compiled type of language, before running will be compiled link to an executable binary file, execution efficiency is very high, in the authoritative performance test, many scenes performance is better than Java, more than Python is a few orders of magnitude:

For more comparison, refer to this website: https://benchmarksgame-team.pages.debian.net/benchmarksgame/faster/go.html

The above is the rich second generation go language birth introduction, more details, please look forward to the following article.

Resources

    • Xu Xiwei: "Go language Programming"
    • Https://golang.org/doc/faq#overloading
    • Https://golang.org/doc/faq#Why_doesnt_Go_have_feature_X
    • Https://zhuanlan.zhihu.com/p/22486908?refer=study-fe
    • Https://benchmarksgame-team.pages.debian.net/benchmarksgame/faster/go.html
    • https://blog.csdn.net/STFPHP/article/details/72617749

If you like the content of the article, you can scan the following QR code to subscribe to the author's public number, pay attention to the latest update, thank you.

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.