Some interesting features in the go language and a view of Go

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

1. Multi-value return

If you need to return a multivalued value in C + +, it is common to pass in a pointer or reference in a function, such as

Fun (int *a,int *b,int *c), but in go, if you need to return multiple values, just write the function like this

1 func test_func () (int,int,int) {2     1; 3     2 ; 4     3 ; 5 6     return a,b,c; 7 }

The last function returns A,B,C

This is actually a long time in Lua, so it's not really something new in go, and there's a feature named in Go with a return value.

Func read_file () (read_count Int,err int) {
....//What the code needs to do
return ;
}

This allows the code of the function to be clearer and more understandable, and to see the function declaration to know what the return value is.

2.0 value initialization, automatic derivation of type

C + + 11 has a new auto, in Go also has: = symbol can be automatically deduced, such as a: = 1,a will automatically be deduced into int type, there is another feature is the Ganoderma lucidum initialization, I think this is most C programmers love, Because of this, we can write fewer lines of code to initialize a variable (in most of my code is also used to initialize the variable), if C + + has this feature, in fact, many errors can be easily found, I see a lot of code because there is no initialization caused by the

3. Garbage collection

This needless to say, is absolutely a sharp weapon, resource management has been a C + + headache problem, of course, automatic garbage collection will certainly bring performance degradation, and even memory leaks (it is said that Java also has a memory leak problem, Python is some, although these have garbage collection mechanism)

4.slices

Plainly slices is a pointer to the bottom of an array, but unlike the array, slices can increase the length when the element is added, and slices a bit similar to the pointer, once the slices is modified, the data that the slices points to will change, There is no way to degenerate arrays into pointers, and if you pass in an array, go copies a copy of the entire array, that is, if there are 100 elements, go copies a copy of the 100 elements.

5:defer

Defer is automatically called when the function exits, we use the Defer Life code snippet, this is to prevent us from forgetting to close the file handle or freeing up resources, the common mistakes that C + + programmers make

1 f,_ = os. Open (file_name); 2 3 defer f.close ()

The function does not immediately execute F after opening the file. Close, but does not execute F until the function has finished executing. Close ()

6: interfaces, custom types and methods

1 classGraph2 {3  Public:4     intgetwidth ();5 6     intgetheight ();7 8     Virtual voidDraw ();9 Private:Ten     intwidth; One     intheight; A}

I always dislike this way of C + +, because put a lot of functions and data together, so that the modern code will become very confusing, and because the existence of virtual function, in the initialization can not be directly used memset or memcpy, if a class has hundreds of variables, Then we need to go to the manual initialization, unlike the C language, the data structure is the original value, can be directly memset initialization, go inside is automatically help us 0 value initialization

In fact, you should know that C + +, the above compiler in the generation of code to help us to separate, such as Getage () will become getage (person &person), in Go is a class divided into three parts, data, methods and interfaces

1Type Graphstruct{2Widthint;3Heightint;4 }5 6 7Func (g *graph) getwidth () (int){8     returnG.width;9 }Ten  OneFunc (g *graph) getheight () (int){ A     returnG.height; - } -  theFunc (G *Graph) Draw () { -Fmt. Printf ("Graph Draw"); - } -  +Type Graph_interfaceInterface{ - Draw () + } A  at func Draw (g graph_interface) { - G.draw (); -}

interface is the declaration of an interface, which is similar to the virtual function of the vptr, you can take the type Graph_interface interface this sentence as a function to join the virtual function table, using this interface can call the incoming parameters of the draw this function (C + + The implementation principle of virtual function is also using this method)

In addition to the function declaration (G *graph) You can bind the data of the class with these methods, there is nothing else to say, public and private data or functions are used to distinguish between the case, but the go inside is different from C + +, C + + If it is private, other classes will not be able to access the variable or function, while go is not accessible to other files, this file can be accessed, a bit like the static C

In fact, the C + + inside of the implementation of these principles I estimate is the same, just show the difference between the grammar, of course, so far I prefer the grammar of go, I think go design ideas more consistent with Linus's statement

"Bad programmers are concerned with code. Good programmers are concerned with data structures and the relationships between them. ”

Git's design is very simple, its data structure is very stable, and has a rich description of the document. In fact, I'm very much in favor of designing code around our data structures, not others, which I think is one of the reasons for Git's success [...] In my opinion, the difference between a good programmer and a bad programmer is that they think the code is more important or the data structure is more important.

I think the C + + programmer wants to read this article: http://www.aqee.net/torvalds-quote-about-good-programmer/

Separating the data structure from these methods will help the programmer to clarify the relationship between the data structure and the function.

7.goroutine

It is said that this is go the biggest "selling point", the online touts the article is also many, but Lua very early has this thing, so I think nothing can be touted, that is, in-line to simulate the single-core computer to run the code snippet, of course, this saves resources, do not have to switch threads

Go Func_name (), so that a function as a goroutine to run, of course, routine is not concurrent, but parallel, so goroutine is in-line simulation of a single-core computer to run code (concurrency and Parallelism differences)

Go routine communication is also very simple, is the use of channel, used UNIX shell should be familiar with this thing, not much to say

The downside of Go:

1. Cannot be combined with C, cannot compile into lib,dll

In short, I think go is still very useful, but so far, did not find what can be embedded into the C language, only c embedded into the go inside, this I would like to cause a lot of companies do not want to use go, I would like to go into a project inside, but found that go can not be embedded in C, You can't compile it into a DLL and give up.

2. It is not designed for concurrency, as it is said on the internet, but improved C + +

Should be able to replace C + + or Java, but the estimate can not be replaced by the B, on the internet compared to go and Erlang, I still think go can not compare with Erlang, go is the mostimproved C + +, and Erlang is born to parallel operation of the birth of the cannot compare , Go is also the use of the object-oriented, process-oriented, and Erlang is inherently process-oriented, everything is a process, even the thread scheduling code is written by itself rather than directly encapsulate the operating system API layer, it is said that the go thread dispatch code only hundreds of lines, and Erlang has tens of thousands of rows, There's a difference here, and even I've always felt that Erlang could have come out alone to do the operating system.

3. Mandatory coding style

Go also has shortcomings, such as mandatory some coding means, if you do not use a variable, will not give you a warning, but will give you a mistake, can not be compiled, it is said to increase the speed of compiling, this surprised me, because I often need to reserve interfaces or variables in the programming, this is good for future changes, Is it effective to save compilation time in this place?? I guess the go designers to force everyone to write good-looking code, this in many can not be compiled by the option to see, go to the programmer's programming style mandatory requirements let me very not adapt to ... Besides, we project more than 100,000 lines of code, the whole project is re-compiled, 10 minutes and 20 minutes is different?? I think most people are cutting out and doing other things.

4. Bizarre grammar

var n int; Is this weird?? Anyway, I'm not used to the way this variable is declared, and curly braces

1     if true {2         // Code 3     } Else if false {4         // Code 5     } Else 6     {7         //code8     }

Need to force the curly braces with the conditional statements together, do not know go Designer is to be unconventional or lazy to write grammar analysis?? So the code does not have c the curly brace on the other side of the good looks, do not know why to force the programmer to write this, but remind me of the original Python indentation also caused a lot of controversy ... I've been arguing this with people on the forum.

5. Is it faster to compile than C + +?

Online Some people say go compiler faster than C and C + +, go in order to increase the speed of compiling the programmer must not be in the code does not use variables, import package if not used will also be prompted to compile errors, but this can really solve the go compile faster than C + +? C + + Because the syntax is complex for compiler designers has been a nightmare, but I do not think go to Lib or obj concept (I did not find the temporary file generated, or I do not generate the wrong way??), Are we going to compile all the import files again when we build a project? What if the project is going to get bigger and compile faster than C + +? If the entire project of C + + is not re-compiled, go will compile faster than C + + advantage?? I remember seeing someone on the internet saying that C + + is compiled slowly because once a file has been added 46 times, he also needs to be opened 46 times, but I think the problem go should also exist, or have been solved??

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.