Why I choose the Go language

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

Here, I do not intend to provoke the language controversy of the war, I am not what Daniel, the language of the accomplishment is not deep, just want to through their actual experience, say why I choose Go in the project.

Experience in other languages

C++

I have had many years of experience in C + + development before touching go. Mainly used in the game server engine development and peer-to, it is a painful and happy period, so that I see any of the program nail problems can be used in C + + this hammer to finalize. But for Internet project development, unless your team's overall C + + technology is NB, and there are strong code specifications, it could be a disaster, not to mention that there are few other people in our existing team that can do this.

Originally, I was going to use C + + in my existing project's push system, and I wrote a web base libtnet in my spare time, but then decided to stop because no one could help me develop it. To my great satisfaction, Libtnet has a game company in use, is now in the internal testing phase, is about to release, I am very much looking forward to their good news.

Lua

In the process of playing, I also learned the language of Lua, and also had the privilege of contacting and perfecting the cloud wind in Lua is not mentioned in C + + the terrorist lua,c++ adhesion layer.

Lua is really a very good language with high performance and fast development. Not only is the game company heavily used in the Internet, because of the popularity of openresty, some companies (including US) are also starting to use LUA for development on the web side. (I'm proud to give openresty feedback on several bugs)

However, because Lua is too short, there are not many libraries, many of them need to be implemented on their own, and it is not easy to write high-performance, high-quality LUA code. In addition, because of its dynamic language characteristics, we also planted a lot of pits, this follow-up in detail.

Python

Before I came to the existing team, they had used python for the entire system development, even the client GUI (which was a disaster for the client's shoes, and later Qt was a pleasant one).

The benefits of Python do not have to be said, from countless companies using it for development know that the library is very rich, concise code, rapid development.

However, after more than two years of Python development in the project, we are getting a lot of problems:

    • Performance, Python's performance is relatively low, and for many performance hotspot code, it is usually implemented in other ways. In our project, we need to sign authentication for any API calls, authentication Service we started using the Tornado implementation, but unfortunately, put on the outside network and did not resist the pressure. So we introduced Openresty, put a lot of high-frequency operation implementation into the Openresty implementation, finally resisted.
    • Deployment, Python library because it is too rich, so our children's shoes introduced a lot of libraries, personally feel that our children's shoes can not be interested in making wheels. Sometimes when we send a version, we will not be able to run the program because we forgot to install a library. This may be related to the lack of mature operations experience in our team, which should be resolved by Salt,puppet this release tool.
    • Quality, we generally think that because of the simplicity of Python code, we can easily write high-quality code, but if there is no good code control, with Python can still write the code of the slag, I even think because of its flexibility, it may be easier to write bad code. This can be said to be the lesson of our team.

Here, I don't mean to spray python, it's really a good language, and I'm able to quickly build prototypes, validate my ideas, and use them all the time. Just in the project, some of our negligence led to the code being out of control, to the point where it had to be refactored.

Why GO?

I said my language experience, and the reason why the project was refactoring, but why go? We can have a lot of other options, such as Java,erlang, or still use Python. I think there are many factors to consider:

    • Static, go is a static language with strongly typed constraints, so we are unlikely to have runtime error such as a runtime type mismatch (such as int + string) in Python. During the compile phase, we can find a lot of problems without waiting for the runtime. (Of course, this static language can do it)

    • Code specifications, many people are more disgusted with the go-mandatory coding specifications, such as the position of curly braces. But I think, because of the mandatory convention, so everyone write out the go code looks similar, do not bother to delve into code style problems. And I found that because of the harmonization of standards, I can easily understand the code written by others.

    • Library support, go Library is very rich, and through go get very convenient to get Github,google code above the third-party library (quality you have to bear), no, it is very convenient to build wheels with go, and the wheels are usually more stable.

    • Development quickly, have to say, when you get used to go development, with go development features very fast, relative to the static language C + +, the development of the efficiency of the fast no words, I think than Python is not bad, and quality assurance. It took us less than one weeks to develop the core functionality of the push service, which has not changed so far and is running stably.

    • Easy to deploy, because it is static, only need to build into a running program can be, deploy the time to throw a file in the past, do not need to install too many dependent libraries like python.

Go features

Go now this way, some people like, some people do not like, I do not know why Google those people put go design so, but I think, since there is a reason, I just need to know what to use, what should not be used on it.

Gc

Go provides a GC, which for C + + children's shoes, greatly reduces the chances of mistakes in memory, just go GC this efficiency is really not good compliment, compared to Java, there is a lot of room for improvement.

So sometimes we have to write code, we have to improve the efficiency of GC according to tuning, such as the use of memory pool to manage the bulk of the slice allocation, the use of no copy of the way to carry out string,slice mutual transfer.

However, go1.3 seems to have a great improvement in GC performance, which makes me more looking forward to this.

Defer

Go defer is actually a person to love and hate things, defer is a very good thing to prevent the disclosure of resources, but misuse of defer will make you face serious memory problems, especially like the following code:

for {    defer func(){        //do somthing    }}

Do not think that go will be done after the call completed defer GC recycling defer inside of the things, in our memory profile, we found that a lot of memory consumption is defer caused. So you need to be very cautious about using them.

But I think, this go should be slightly improved, in go1.3 inside, also has the optimization to the defer.

Error

Perhaps the error is a controversial thing, the modern way of exception there? But I think the error can be very clear to tell the user that the function will have an error to return, if you use exception, unless the document is detailed enough, I really do not know where to jump out of an exception.

It's just that go offers a exception-like Defer,panic,recover, which is where to make a fuss.

In fact, this article I think has been explained very well, the convention of Go Program is external API use error, and internal error handling can use Defer,recover and panic to simplify the process.

In fact, this is the same as my usual programming guidelines, when the team in the development of Python, we have explicitly asked the library to provide external APIs need to use the return value to represent the error, but internally can use the Try,catch exception mechanism.

Interface

Go provides interface for abstract programming. The most common example of an interface is the duck story, "When you see a bird walking like a duck, swimming like a duck and barking like a duck, the bird can be called a duck."

In Go, interface is a collection of methods, and if an object implements those methods, the object can be even the interface. With interface, we can easily implement non-intrusive programming and replace the module function.

For long-time immersion in C + + and Python children's shoes, all of a sudden to use interface to solve the abstract problem, may be very uncomfortable. But when you get used to it, you'll find that interface is really flexible and convenient.

Write to the back.

When it comes to use, we need to know where the go is going to be, for example we're using go refactoring for the API service now, and we're not stupid enough to replace openresty with go.

In short, go is a very new language, there are already many companies in the country began to eat this crab, there are examples of success, and we are beginning this journey.

Finally, I enclose a ppt:https://qing.wps.cn/l/b76667b40bdb4b7c91f2b3920a7f4780 that I wrote to my colleague in the past.

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.