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.

Author Openkk 2012-03-04 18:26:58

Wen/windstorm

It hasn't been updated for some time. Recently busy with a server+client project, the Client is an Android phone, presumably also a few decades of scale. Server is a pure Golang implementation, no Apache or Ngix Web server, no database, and wrote a file management Module,handle this small-scale service is no problem. Calculate down contact Golang also have four months, intermittent write some things, here record the experience bar

First of all about why use Golang. 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. Before there was a project similar to the architecture and scale, in order to save time, there was little experience in server platform programming me, on the server side choose to do 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

    • High performance, fast speed
    • Simple and concise, need to remember the language less details, development quickly (C)
    • Flexible, developing fast, Python-like list,map and other common data structure support (Python)
    • Complete module support, module is also easy to get started (Python)
    • Programmer-friendly Parallel architecture (Erlang)
    • Security, 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.

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

Of course, Golang attracted me not because it was made by Google or because it was a designer, but because Golang really did what it claimed to be. Golang is just like the perfect combination of C and python, if you're a Python enthusiast, and you pursue code speed and parallelism, simply put, Golang is designed for you. Golang 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 Oo), all with a struct-centric, no exceptions (Oh yes!), still have pointers, and so on. However, Golang has absorbed the essence of many new languages and has its own unique design. Like what:

1. Retain but significantly simplify pointers

Golang retains the difference between the value of C and the pointer, but it makes a lot of simplification for the cumbersome use of pointers, introducing the concept of reference. So in Golang, you almost don't have to worry about getting all sorts of errors because of the direct operation 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, etc.

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

4. Interface

Golang's most impressive feature 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, Golang interface 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 Golang, you can provide duck typing features for general dynamic languages, while still capturing obvious errors in compile.

5. OO

Golang is not an object-oriented language in nature, it is still procedural. However, in Golang, 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 in Golang, is the ordinary struct operation.

6. Goroutine

This is almost a golang of the signature features, I do not want to mention more. 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 Golang, triggering a routine is as simple as Erlang spawn. Basically to master the Golang, 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

Compared with C, Golang is completely a modern language, native supported 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 Golang. What I want to say is, is not exaggerated at all, even Golang's introductory learning to improve, are much lower than the other language threshold too many. In a time when most people have a background of C, for Golang, from getting started to being able to get started on projects, it's up to half a month. Golang gives the feeling is too direct, anything directly, read the source code directly, write their own code is also direct.

Have a friend to protest, you blow golang 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 Golang, you can refer to the following Yufeng written in this article, System programming language of Tomorrow Star-go (http://blog.yufeng.info/Go.pdf).

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

Okay, time to 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.