Go language study notes [2] GO and C are some differences in the code

Content from: Clear Go Language code style, simple

Go language syntax similar to the C language, so familiar with the C language and its derived languages ( C ++ , C # , in Objective-C, etc.) who will quickly become familiar with the language.

Some C language syntax makes the code more readable even reduce ambiguity occurs. Go whichever language based on the C language of the essence, discard the dross, the C language is more prone to error wording is adjusted accordingly compiled tips.

1) removing the cyclic redundancy brackets

Go language on the basis of combat experience of many masters of the birth, to C language syntax some redundancy, in addition to the cumbersome part.

The following code is cycle value C language:

. 1  // C language for numerical loop 
2  for ( int A = 0 ; A < 10 ; A ++ ) {
 . 3      // loop code 
4 }

In the Go language, this cycle becomes:

. 1  for A: = 0 ; A < 10 ; A ++ {
 2      // loop code 
3 }

for both sides of the brackets are removed, int is simplified statement :=, the compiler directly through the value of the right type of variable is derived and obtained a statement.

2) removing the redundant expression brackets

The same simplification may be reflected in the decision statement, the following C language statement determination.

. 1  IF (expression) {
 2      // expression is established 
3 }

 

In the Go language, without adding the expression in parentheses, as follows:

. 1  IF expression {
 2      // expression is established 
3 }

3) mandatory code style

Go languages, must be followed by a left parenthesis statement does not wrap. Other styles of braces will be treated as code compilation error. This feature will make the beginning of a number of developers have not used, but with the language continues to Go familiar, developers will find a unified style so that everyone at the time reading the code to focus on solving problems rather than on code style .

At the same time the Go language also provides a set of formatting tools. Some Go language development environment or editor when you save will use the formatting tools to format the code, so that when the code has been submitted to a unified format code.

4) no longer entangled in and i ++ ++ i

In the Go language, the increment operator is no longer an operator, but a statement. Therefore, in the Go language increment only way to write:

1 i++

 

If written preincrement ++iafter, increment or assignment a=i++will result in a compilation error.

Guess you like

Origin www.cnblogs.com/Annetree/p/12486444.html