Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to declare syntax in Go

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "how to declare grammar in Go", which is easy to understand and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn this article "how to declare grammar in Go".

C variable

First of all, let's talk about the syntax in C. C uses an unusual and ingenious way to implement declaration syntax. Instead of using any special syntax to describe types, we write an expression that contains two parts: the declared variable and the type of variable.

Int x

The above line declares a variable x of type int. In general, to figure out how to write the type of a new variable, you can first write an expression with a primitive type variable, and then put the primitive type on the left and the expression on the right.

Therefore, the following statement:

Int * pint twita [3]

It describes p as a pointer to the int type because the type of'* p'is int. And an is an array of type int, because the type of'a [3]'(ignore the subscript value 3 here, which just indicates the size of the array) is int.

What about the function? In the beginning, C's function declaration wrote the type of parameter outside the parentheses, like this:

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

Once again, we can see that main is a function because the expression main (argc, argv) returns a value of type int. Now people are used to writing like this:

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

But the basic structure is the same.

This ingenious syntax idea works well for simple types, but it can be confusing once the type becomes complex. A very classic example is to declare a function pointer. By following the rules, you get the following:

Int (* fp) (int a, int b)

Fp is a pointer to a function, because if you write an expression (* fp) (a, b) you will call the function and get a value of type int. What if one of the input parameters of fp is itself a function?

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

It becomes difficult to read.

Of course, we can remove the parameter name when declaring a function, then the main function can be declared as:

Int main (int, char * [])

Let's recall that this is what argv declared.

Char * agrv []

It can be confusing to declare a type like char * [] by putting the variable name in the middle.

Then let's see what the declaration of the fp function looks like if we remove the name of the argument:

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

No matter where you put the variable name inside, it is not so clear. For the first input parameter:

Int (*) (int, int)

I don't think it's easy to see at a glance that you're declaring a pointer to a function. Further, what if our return value is also a function pointer?

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

It is impossible to see what the declared fp is at all.

You can construct more such detailed examples yourself, but these illustrate some of the difficulties that C's declaration syntax may introduce.

However, there is one more point to make. Because the syntax for types and declarations is the same, it is difficult to parse expressions of intermediate types. This is why C's type conversions are always enclosed in parentheses:

(int) M_PI

Go syntax

Non-C family programming languages usually use different syntax for declaring types: variable names usually come first, followed by a colon. So our example above looks like this:

X: intp: pointer to inta: array [3] of int

These statements are clear and detailed if you read them from left to right. The Go language was inspired by this, but for the sake of brevity, colons and some keywords were removed:

X intp * inta [3] int

In this example, [3] int does not seem to correspond directly to how to use an in an expression. We'll talk about pointers in the next section. ) you can get clear results through separate syntax.

Now let's think about the function. Let's write this declaration in the form of Go, although the real main function in Go has no arguments:

Func main (argc int, argv [] string) int

On the surface, this is no different from the C language, except that the character array is changed to a string form. But it reads smoothly from left to right:

The function main needs to pass in an integer and string slice and return an integer. (translator's note: it was not until the translator saw this article that the translator realized that it was so smooth to write and read in this way. )

Even if you leave out the variable name, it's still clear-- because there's no change in location on the type declaration, there's no confusion.

Func main (int, [] string) int

This left-to-right style has one advantage: it behaves appropriately even as the type becomes more and more complex.

Take an example of declaring function variables (similar to function pointers in C):

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

Or if f returns a function, you'll be surprised again at this silky smoothness. ):

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

It still reads smoothly from left to right and is obvious when the variable name is declared.

The syntax differences between types and expressions make it easy to write and invoke closures in Go:

Sum: = func (a, b int) int {return a + b} (3,4)

Pointer

The pointer guy always acts a little different. Looking at the following arrays and slices, for example, Go's type syntax places square brackets to the left of the type, but the assignment expression syntax places it to the right of the expression:

Var a [] intx = a [1]

To give you a familiar feeling, Go pointers also continue the * symbol in C, but we can't simply reverse the pointer type. So the pointer is used as follows:

Var p * intx = * p

We cannot simply and rudely change it like this:

Var p * intx = p *

Because the suffix will be confused with multiplication. Then maybe we can use ^, for example:

Var p ^ intx = p ^

But the same symbol already has other meanings, and types and expressions always complicate things in many ways when it comes to prefixes and suffixes. For instance,

[] int ("hi")

This is a way of writing, but once it starts with *, it must be enclosed in parentheses:

(* int) (nil)

If we are willing to give up * as pointer syntax, then these parentheses are not necessary. (translator's note: but can there be better pointer syntax? no, no, no. no, no, no. )

So the pointer syntax of Go is similar to the familiar C language, but this association also means that we have to use parentheses to eliminate the differences between types and expressions in syntax.

Overall, we believe that Go's type syntax is easier to understand than C's, especially when things get complicated.

The above is all the content of the article "how to declare Grammar in Go". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report