From C + + to go

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

From C + + to go

Just started to touch the Go language, read two of the Go language books, from the perspective of C + + developers to see the new features of the go language, said his feelings deeper points:

Concurrent programming

The Go language level supports the process, which transfers the concurrent business logic from asynchronous to synchronous, greatly improving the development efficiency.
In C + +, the current mainstream scenario for concurrent programming is event-driven (single-threaded/multithreaded/multi-process models, etc.), and event-driven requires an IO multiplexed dispatcher (select/epoll), which causes the business logic to be disconnected at the code level as an asynchronous model, such as:
1). First a business code
2). Call IO (business break)
3). Subsequent processing logic after IO is completed;
And go in the support of the process to make this kind of development work easier, in accordance with the sequence of synchronous writing business logic, encountered IO also does not matter, a thread can be created into millions of co-process, this association is blocked to run the next, do not need to apply the code level to be responsible for the processing of IO follow-up scheduling;
The advantage of Go is to encapsulate the event mechanism into CSP mode, which is convenient for programming, but it needs to pay the overhead of goroutine scheduling, compared to the libevent to encapsulate the bottom layer or call the library.
All system invoke operations provided by the Ps1:go language standard library (including, of course, all synchronous IO operations) will give the CPU to other goroutine;
PS2: Read the online experience data, synchronous and asynchronous development efficiency (not operating efficiency, refers to the caller speed) is almost 4:1, hey, this data is very exciting ~ ~
For more information on event-driven vs. co-process concurrency, see the answer to the No-birds Rio:
https://www.zhihu.com/question/19585576/answer/12424447

Garbage collection

No doubt this is easy to use, with garbage collection, no need for developers to control the release of memory, so as to avoid a lot of problems (re-release, forget to release memory, access to freed memory, etc.);
Of course, C++11 introduced smart pointers (UNIQUE_PTR, etc.), if applied in the application of the general, can also achieve similar garbage collection purposes;
GC brings the problem is also some, will cause STW, there will be a program to stop the scheduling of the stutter;
GO1.5 's GC uses various means to greatly reduce the time of STW. The go language is officially guaranteed to have up to 10 milliseconds of scheduled downtime due to GC in the 50-millisecond Go program runtime.
(ref:http://www.infoq.com/cn/articles/2015-review-go)

function multiple return value

This is nothing new in Python, but for C + +, to implement a function to return multiple data, either encapsulate a struct, or only through the function of the parameter implementation;
Multiple return value this thing, in the code word can enhance the mood of pleasure Ah, think, in order to return multiple values of the scene, no longer find a place with a structure package, direct return, more direct:
Func getName () (FirstName, MiddleName, LastName, nickname String) {
Return "may", "M", "Chen", "Babe"
}

Error handling

Referring to the multiple return value, then the error handling, the estimation of multiple return value of the most applied scenario is the second parameter return to the error state of the function, such as the following notation is very common:
If result, OK: = Moremagic (); OK {
/do something with result /
}
C + + error handling is generally determined by the error code to determine whether a function is called correctly, so the error-handling code line of Go is reduced and looks elegant compared to C + + +;
Go introduces 3 keywords (defer, panic, and
Recover) is used for standard error-handling processes, and the introduction of defer keywords to ensure that the error-handling code can be invoked when errors are found, and will not be missed due to changes in the Business branch logic;

Of course, look at the comparison with who, Python practitioners think that there is no use of Try catch exception handling mechanism, so that error handling seems cumbersome;
Russ Cox points out that the go language is designed for large software, and the return error of the go language is undeniable and is not very convenient for callers, but doing so will make it obvious where errors can occur in the program. For small programs, you may only want to print out errors and exit the program. For some very sophisticated programs, depending on the anomaly, the source differs, the program reacts differently, which is common, and in this case, the try + catch approach is verbose relative to the error return pattern.
Ref
The error handling mechanism of Go language causes controversy
Http://www.infoq.com/cn/news/2012/11/go-error-handle

The status of the function is improved

The function appears as a "type" and becomes a class-one citizen; You can define a function type, assign a function to a function variable, and then pass it in the business chain, which can only be done using std::function in C + +;
You can also use anonymous functions (which correspond to lambda expressions in c++11) to support functional programming at the language level, allowing for more flexible control and management of the program.

Mandatory encoding Specification

The system is bigger and longer, the code quality inevitably drops; The code style of the developer is inconsistent, causing the program to be filled with strange naming and the organization of the class;
Yes, it is a company will have code specifications, but it is written on paper, whether it really follow the implementation, it is really difficult to say, what late scan, do not change to the online?
For the project, go to enforce the code specification, refreshing, from the naming, to the organization of code arrangement has a clear stipulation, does not conform to can not compile through! This is really good, code engineering is not to realize the place of individuality;

Why are you doing this after the grammar?

All of the above are the advantages of go, to see a good one; but the syntax of go, such as variables, function declarations and definitions, is a kind of post-type, compared to our common language syntax, which is somewhat unaccustomed;
Why are you doing this?
Rob Pike, one of the creators of the Go language, explains this question: it's not about being different, it's about being more clear and understandable. Especially when the type is more complex, go's type syntax is easier to understand than C.
See:
https://www.zhihu.com/question/21656696/answer/19027040

Finally, put a few points of the philosophy of Go language:
The go language sets the strengths of many programming paradigms and blends them in a unique way. Programmers can use their favorite style to design the program.
The go language has a clear and near-strict coding standard relative to the design rules. We can format the code according to official specifications using the "Go fmt" command.
The go language is a programming language that emphasizes software engineering. It comes with a very rich range of standard commands that cover every aspect of the software lifecycle (development, testing, deployment, maintenance, and so on).
The go language is the programming language of the cloud computing age. It focuses on high-concurrency programs and is designed to strike a balance between efficiency and operational efficiency.
The Go language advocates exchanging data instead of sharing it. Its concurrency primitives Goroutine and channel are two of the most concurrent programming tools. The go language also offers a wide range of synchronization tools for programmers to choose from. However, the latter is not supported by the language level.
Http://www.infoq.com/cn/articles/go-language-introduction

Posted by: Big CC | 26jan,2016
Blog: blog.me115.com [Subscribe]
Github: Big cc

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.