Why Go language to type on the back

Not to be different. But in order to more clearly understand. 

Rob Pike Go once explained in an official blog this issue (the original address: http://blog.golang.org/gos-declaration-syntax ), simple translation is as follows (wrong with the level of translation limited forgive me):

Introduction

Go new language often very puzzled why the declaration syntax of the language (declaration syntax) and will be different from the traditional C language family. In this blog, we will conduct a comparison, and make answer.

C syntax

First, take a look at the C syntax. C uses a clever and unusual declaration syntax. When you declare a variable, simply write the expression with a target variable name, and then specify the expression itself can type in an expression. For example:

  int x;

The above code declares a variable x, and the type of int-- i.e., expression x int. In general, in order to specify the type of the new variable, we have to write an expression containing the variables we have to declare, this expression operation resulting value belong to some kind of basic types, we call this basic type of written expression He left. So, the following statement:

  int * p;
int A [. 3];

indicates p is an int type pointer, * p type because of an int. And a is an int array as a [3] is of type int (never mind index value appear here, it is only for the specified length of the array).

Next we look at the case of a function declaration. C function declaration regarding the type of the parameter is written outside the parenthesis, as follows:

  int main (argc, the argv)
int argc;
char * the argv [];
{/ * ... * /}

As mentioned above, we can see the main reason why the function, because the expression main (argc, argv) returns int. In modern notation we are so written:

  int main (int argc, char * argv []) {/ * ... * /}

Although it looks a little different, but the basic structure is the same.
Overall, when the type is relatively simple, the C syntax is very clever. But unfortunately the type of start once complex, this C syntax soon people confused. Notable examples such as function pointers, so we have to be written as follows:

  int (* fp) (int A, int b);

here, fp is a pointer to the reason why is because if you write (* fp) (a, b) this expression will call a function that returns an int. If and when a parameter fp of itself is a function of what would happen then?

  int (* fp) (int ( * ff) (int x, int y), int b)

This may point to read difficult.
Of course, we can not declare a function is specify the name of the parameter, so the main function can be declared as:

  int main (int, char * [])

Recall that before argv is below

  char * argv []

you have you are not found from the "middle" declared variable name and then remove its variable type of structure? Although it is not obvious, but you declare a char * [] types of variables, when even need to be inserted into the middle of the name of the variable type.
We look at the parameters of fp what will happen if we do not name:

  int (* fp) (int (*) (int, int), int)

This is something difficult to understand not just a place to remember the name of the parameter that was originally put in the middle of

  int (*) (int, int )

it places even more confusion lies might even have wondered if it turned out to be a function pointer declaration. We then see if the return value is also a function pointer type then what will happen

  int (* (* fp) ( int (*) (int, int), int)) (int, int)

It is already difficult to see on fp declares.
You can also build more complex than this example, but it was enough to explain some of the complexities of the statement of the C syntax introduced.
Another point to be noted that, due to the type declaration syntax and grammar are the same, to the middle with analytical expression may be some type of difficulty. This is why, the reason C in a cast and when should always type in parentheses, like this

  (int) M_PI

Go grammar

non-C family of languages typically use a different type syntax in the statement. Usually appear first name, then often followed by a colon. According to this write, the example cited above, we will become follows:

  the X-: int
the p-: pointer to int
A: Array [3] int of

such a statement even if somewhat lengthy, when at least is clear - you just to be read from left to right on the line. Go language program is used as a basis, but the pursuit of simplicity, Go language lost the colon and remove some of the key words, became follows:

  the X-int
the p-* int
A [3] int

in [3] int usage and expression is not a direct correspondence (in the next section we'll come back to discuss the issue pointers). At this point, you get the code to enhance the clarity of the terms, but the cost is the need to be treated differently on grammar.
Let us consider the function. Although the Go language, the main function is virtually no argument, but let's declare main function of a transcript copy it before:

  FUNC main (int argc, argv * [] byte) int

first glance and C is no different, however, from left read the words right bad.
The main function accepts a pointer and an int and returns an int.
If at this time the parameter name removed, it is very clear - as always in front of the parameter name type, so it will not cause confusion.

  func main (int, * [] byte) int

a value from left to right style of this statement is that when the type becomes more complex, it is still relatively simple. Here is a declaration of the variable function (corresponding to C language function pointer)

  F FUNC (FUNC (int, int) int, int) int

or when it returns when a function:

  F FUNC (FUNC (int, int) int, int) func (int, int) int

above statement is very clear to read, from left to right, but exactly which one is the current variable name is declared also easy to understand - because the variable name is always in the first place.
Type syntax expression syntax and how much difference in the Go language calls such closures also easier:

  SUM: = FUNC (A, B int) int {A + B} return (. 3,. 4)

Pointer

Pointer some exceptions . Note that the array (Array) and slice (Slice) in, the type syntax Go to brackets on the left type, but the expression in the square brackets syntax but placed to the right:

  var A [] int
X = A [1]

Similar, Go pointer follows the C * notation, but we also write when you declare a variable name * in the right, but in an expression but have to put left * left:

  var the p-* int
the X-* = p

can not be written as follows

  var p * int
the X-= p *

because * might confuse and multiplication suffix, maybe we can use Pascal's ^ marks, like this

  var p ^ int
the X-p ^ =

maybe we really should such as above into the ^ * (symbol such a change of course also have to change xor operation), because the type and expressions * prefixes are actually the better thing to do a bit more complicated, for example, although we can write like this

  [] int ( "hi")

but in the conversion, if the type is the beginning of the *, you have to add parentheses:

  (int *) (nil)

if we are willing to give up one day with a pointer * grammar, then the above brackets can be omitted.
Visible, Go, and C pointer syntax is similar. But this similarity also means that we can not completely avoid the situation in the grammar sometimes in order to avoid ambiguity and type of expression need to add brackets.
All in all, despite the shortcomings, but we believe that Go's type syntax than C's easy to understand. Especially when the type is more complex.

https://www.cnblogs.com/lvdongjie/p/6511124.html

Guess you like

Origin www.cnblogs.com/findumars/p/11832791.html