Experience of using the go language for a period of time

Source: Internet
Author: User

From: http://www.kunli.info/2012/03/03/golang-feeling/

 

Since then, I have thought that if a language can combine the advantages of C and python, that is

    • Fast and High Performance
    • Concise and clear, with few language details to be noted and rapid development (c)
    • Flexible, fast development, and support for common data structures such as list and map in Python (Python)
    • Comprehensive module support and easy-to-use modules (Python)
    • PairProgramMember-friendly parallel architecture (Erlang)
    • Security, most problems can be eliminated in compile time (C minus pointer)

That is basically the language at the system level and network level that is most appetizing to me. Then I found go.

Golang is a new language. So far, the first official version has not been released. Golang's designers were Robert griesemer, Rob Pike, and Ken Thompson, who designed C and UNIX in that year and later joined the plan9 team. Golang's design philosophy is clear, that is, to combine the programming accessibility of dynamic language with the security efficiency of static language. If you want to learn more about golang's development history and complete objectives, refer to the golang FAQ.

Of course, golang attracted me not because it was produced by Google, nor because it was designed by Daniel, but because golang really achieved what it declared. Golang is like a perfect combination between C and python. If you are a python enthusiastCodeIn short, golang is designed for you. Golang has a strong legacy of C, and tries to shield the impact of C ++ and Java. For example, there is no independent OO system (not to say that it cannot be OO), and everything is centered on struct, no exceptions (oh yes !), There are still pointers, and so on. However, golang draws on the essence of many new languages and has its own unique design. For example

1. Reserved but significantly simplified pointer

Golang retains the difference between the value of C and the pointer, But it simplifies the tedious usage of the pointer and introduces the reference concept. Therefore, in golang, you do not have to worry about various errors caused by direct internal operations.

2. Return multiple parameters

Do you still remember how many pointers have to be opened in order to return multiple parameters in C to the target function for its operation? This is completely unnecessary in go. In addition, the support of multiple parameters makes go do not need to use the tedious exceptions system. A function can return the expected return value with an error. After the function is called, the error information is processed immediately, which is clear and clear.

3. built-in basic data structures such as array, slice, and map

If you are used to simple list and dict operations in Python, you will not feel lonely in golang. Everything is so familiar and more efficient. If you are a C ++ programmer, you will find that you have found the STL vector and map friends.

4. Interface

Golang's most impressive feature is interface design. Any data structure, as long as the function defined by the interface is implemented, the interface is automatically implement, without the lengthy class statement like Java, it provides a lot of flexibility in design and OO abstraction, so that your code is very clean. Never think that you are used to the Java method of adding implements. It feels okay. When the interface design becomes more and more complex, countless bugs are waiting for you.

At the same time, the golang interface can be used to represent any generic. For example, an empty interface can be a string, an int, or any data type, because these data types do not need to implement any function, they naturally meet the definition of the null interface. With golang's type assertion, the duck typing feature is available in general dynamic languages, while the obvious errors can still be captured in compile.

5. Oo

Golang is essentially not an object-oriented language, but procedural. However, in golang, you can easily do most of what you can do in other oo languages and use simpler and clearer logic. Yes, here, class is not required, it can still be inherited, it can still be polymorphism, but the speed is much faster. In essence, oo is a normal struct operation in golang.

6. goroutine

This is almost one of golang's signboard features, and I don't want to mention it any more. If you do not know about goroutine at all, you only need to know that this is a super lightweight thread-like thing, but through it, you do not need complicated thread operation lock operations, you can play with basic parallel programs without the need for care scheduling. In golang, triggering a routine is as simple as Erlang spawn. Basically, to master golang, the memory model with goroutine and channel as the core must be understood. But please rest assured that it is really very simple.

7. More modern features

Compared with C, golang is a modern language. It supports Unicode, garbage collection, closures (yes, similar to functional programming language), function is a first class object, and so on.

Here, you may find that I have used many easy, simple, and fast adjectives to describe golang's characteristics. What I want to say is that it is not an exaggeration at all. Even golang's entry-level learning has been improved, which is much lower than other languages. In an era where most people have a C background, golang can start a project for at most half a month from getting started. Golang gives people the feeling that it is too direct, everything is direct, readSource codeYou can directly write your own code.

A friend is protesting. If you blow golang so well, does it have no disadvantages? Yes, of course. However, compared with its advantages, I think many shortcomings are due to the fact that the entire language is too new and immature and can be solved over time. In contrast, it can be tolerated. If you want to learn more about golang's advantages and disadvantages, refer to this article written by Yufeng below.Article, SystemProgramming LanguageTomorrow's star-Go (http://blog.yufeng.info/go.htm.

Some friends want to say that golang is so good, why is it useless? I 'd like to say that there are more wonderful things in the world than you think. Golang is used by Google for YouTube databases, and is used by more and more foreign companies (Most startups) for backend development. Even in tianchao, there are cloud application companies that fully use golang for service development. It can be said that with the upcoming Official launch of go 1, golang's use scope should become wider and wider.

Good, summary time

If you are a fan of Python and dynamic languages, go may not surprise you a lot. It depends on the nature of your project, considering that python currently uses C for core operations in many places, the speed is not a big problem in most cases. Scalability is a problem, but not everyone will.

If you are a C enthusiast, it is strongly recommended that you study and use go. Go can call C/C ++ programs, but it provides too much convenience, with a slight sacrifice in speed, but it is not big. In most cases, go can bring you the performance comparable to that of C. For some scenarios where the performance is too critical, you can use CGO to match go and C.

If you are a Java enthusiast, unless you have to use a Java platform such as Android, we recommend that you try to learn go, the difference in development is as obvious as the tractor with 1 ton of stones and the Porsche 911, while Java can give you, go can give you better.

If you are a C ++ enthusiast ,! @ # $ % ^ & *, Congratulations, at least your IQ should be okay. Life is short, so hurry up and get rid of the bitter sea of C ++. The time you spent learning 89 C ++ advanced features is estimated to have written 64 open-source projects using go.

If you are a C and Python enthusiast like me, you have no special passion for dynamic languages ...... What else do I need to say?

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.