Use the Go language for some time

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

Recently busy with a server+client project, the client is an Android phone, presumably also a few decades of scale. Server is a pure Go language implementation, no Apache or ngix this Web server, there is no database, wrote a file management module, to deal with this small-scale service is no problem. It's been four months to get into the go language, and something has been written off and on, and here's a record of your experience.

Let's start with the go language. I am a person who is neat to the language, once was a c+python of the firm cry, the most often say is, as long as the proficiency of these two, what can be dealt with, there is no Java and C + + (purely refers to my domain). The core code with C, fast, need to remember less language details; the perimeter is glue with Python, flexible and concise, and any module is easy to use and perfect to match. Java's cumbersome, C + + countless useless features, all let me only when I have to use the time to use. Objective-c is another language I admire, the problem is not cross-platform, too closed.

Unfortunately, in this very fast-paced era, not all cases are suitable for C. Prior to a project is similar to the architecture and scale, in order to save time, there was little experience in server platform programming me, on the server side of the choice with Django, Apache, MySQL, mature, community active, but also python as the main development language, these are the reasons for this choice. To tell the truth, after a few months past, looking back, this is not a pleasant experience. Django is a good architecture, chatty, and chatty sometimes mean bloated, a variety of configurations, too tight module coupling to the introduction of third-party tools Limited, the self-brought ORM is not good use. I've never had a server configuration before, and I have a headache for the trivial things that are brought about by Apache's configuration and efficiency. Overall this part took me a lot of time, a novice learning server programming must pass the process, also have to toss Django and Apache unnecessary time wasted, much of the flexibility of Python to offset the rapid development. And once the server is online, some of the bugs that come with dynamic language can be annoying. For the ordinary university laboratory this does not have perfect server debugging condition, basically is changed on the line with, some hidden bug to some conditions branch will trigger, once in the middle of the run, change up also trouble.

Since then, I have been particularly interested in a language that combines the virtues of C and Python, which means

▲ fast speed, high performance

▲ concise and clear, need to remember the language less details, rapid development (c)

▲ Flexible, develop fast, python-like list,map and other common data structure support (Python)

▲ Perfect Module Support, module is also easy to get started (Python)

▲ Programmer-friendly Parallel architecture (Erlang)

▲ Safety, most of the problems can be eliminated in compile time (C minus pointer)

That's basically the language of system-level and network-level programming most to my liking. Then I found the go.

The go language is a new language, so far the first version of the official version has not been released. The Go language designers are Robert Griesemer, Rob Pike and Ken Thompson, who designed C and Unix, later in the Plan9 team. The go language design concept is very clear, is the dynamic type language programming ease and static type language security efficiency combined. If you want to learn more about the history of the Go language and the complete goals, please refer to the Go language FAQ.

Of course, the go language attracts me not because it is made by Google, not because its designers are Daniel, but because the go language really does what it claims to be. The go language is like the perfect combination of C and Python, and if you're a Python enthusiast, and you pursue code speed and parallelism, then simply say, the go language is designed for you. Go language has a very strong legacy of C, as far as possible to block the impact of C + + and Java, such as no independent OO system (not to say not oo), all with a struct-centric, no exceptions (Oh yes!), still have pointers, and so on. However, the go language draws on the essence of many new languages, with its own unique design. Like what

1. Retain but significantly simplify pointers

The go language retains the difference between the values in C and pointers, but it makes a lot of simplification for the cumbersome use of pointers, introducing the concept of references. So in the go language, you almost don't have to worry about getting all sorts of errors due to the direct manipulation of the inside inch.

2. Multi-parameter return

Remember in C in order to give back multiple parameters, have to open up a few pointers to the target function to let it operate it? This is completely unnecessary in go. and multi-parameter support allows go without the cumbersome exceptions system, a function can return the expected return value plus error, call the function immediately after processing the error message, clear.

3. Built-in basic data structures such as Array,slice,map

If you're used to simple list and dict operations in Python, you won't feel lonely in the go language. 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 of the friend.

4. Interface

The most impressive feature of the Go language is the interface design. Any data structure, as long as the implementation of interface defined functions, automatically implement the interface, there is no lengthy class declaration like Java, provides flexibility too much design and OO abstraction, so that your code is also very clean. Do not think you are accustomed to the Java kind of a implements way, feel OK, and other interface design more and more complex, countless bugs are waiting for you behind.

At the same time, because of this, the interface of the go language can be used to represent anything generic, such as an empty interface, can be a string can be an int, can be any data type, because these data types do not need to implement any function, Nature satisfies the definition of empty interface. In addition to the type assertion of the go language, you can provide duck typing features in general dynamic languages, while still capturing obvious errors in compile.

5. OO

The go language is not an object-oriented language in nature, it is still procedural. However, in the go language, you can easily do most of the things you can do in other OO languages with simpler and clearer logic. Yes, here, you don't need class, you can still inherit, you can still be polymorphic, but it's much faster. Because in essence, oo is a common struct operation in the go language.

6. Goroutine

It's almost a sign of the go language, and I don't want to mention it. If you don't know goroutine at all, then you just need to know that this thing is super lightweight like a thread, but through it, you don't need a complex threading operation, you don't need care scheduling, you can play basic parallel programs. In the go language, triggering a routine is as simple as Erlang spawn. Basically to master the Go language, the memory model with Goroutine and channel as the core must be understood. But please be assured that it is really very simple.

7. More modern features

and c comparison, go language is completely a modern language, native support Unicode, garbage collection, Closures (yes, and functional programming language similar), function is first class object, and so on.

See here, you may find that I used a lot of easy, simple, fast and other adjectives to describe the characteristics of the go language. What I want to say is that it is not exaggerated at all, even the introduction of Go language learning to improve, are much lower than the other language threshold too many. In a time when most people have a C background, for the go language, from getting started to being able to get started, it's up to half a month. Go language gives a person's feeling is too direct, what all direct, read source code direct, write own code also direct.

Have friends to protest, you put the go language so good, don't it have shortcomings? Yes, of course, but with its advantages than, I think many shortcomings are because the whole language is too new, immature, with the passage of time can be resolved, in contrast can endure. If you want to learn more about the pros and cons of the Go language, you can refer to the following Yufeng written in this article, the System programming language of Tomorrow Star-go (http://blog.yufeng.info/Go.pdf.

There are friends to say, go language so good, why no one use? I want to say, broaden your horizons, the world is more exciting than you think. The go language is Google's database for YouTube, and is used by a growing number of foreign companies (most startups) for back-end development, even in the celestial world, as well as cloud applications that are fully developed with Go language services. It can be said that with the upcoming launch of Go 1, the use of the go language should be more and more extensive.

Summarize

If you're a python and dynamic language enthusiast, go doesn't have to be a big surprise for you, depending on the nature of your project, and the speed is not a big problem in most cases, given that Python currently uses C for core operations in many places. Scalability is a problem, but not everyone will encounter it.

If you are a C enthusiast, it is highly recommended that you learn and use go. Go can call the C + + program, but also provides too much convenience, the speed of a slight sacrifice, but not large. In the vast majority of scenarios, go can bring you comparable to C performance, and for some of the actual performance is too critical to the occasion, can also be CGO let go and C collocation.

If you are a Java enthusiast, unless you are doing Android this has to use the Java platform, it is also recommended that you try to learn go, the development of the sense of difference as compared with 1 tons of stone tractor and open the Porsche 911 so obvious, and Java can give you, go can give you better.

If you are a C + + enthusiast,!@#$%^&*, congratulations, at least your IQ should be no problem. Life is too short to escape the misery of C + +. The time you used to learn 89 C + + advanced features, it is estimated that 64 open source projects have been written with go.

If you are a fan of C and Python like me, there is no special love for dynamic language ... What else do I have 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.