C++,python,golang Control Study-02

Source: Internet
Author: User
Tags goto
This is a creation in Article, where the information may have evolved or changed.
main function
There is only one main function in go, and the main function must be in package main. The main function has no return value and no parameters, and if you want to get parameters passed from the command line, there are other packages that resolve the issue.
The main function in C + + can have parameters or return values. And when the declaration has a return value, in the main function body can not write, the compiler will help you fill.

Although Python has something like main, in the code we can see if __name__ = = ' __main__ ': such code, but essentially Python does not need the main function. When a python file is treated as if it can be directly

When running the module, its __name__ property will be a special default value of __main__, that's all.


Structure Control
First, if
    Go if it looks like this:
    If condition {  <-{is mandatory, and can only be in this line
      & nbsp ...
   }
    from its table display form, you do not need parentheses to wrap the condition, and it contains a pair of necessary curly braces. It looks like it's C + +. and Python.
    C + + code:
    if (condition) {
        ...
   }
    Python code:
    If condition:

...

The IF statement in go can set a local variable that acts on the If block only.

If err: = chmod (777); Err! = Nil {
The role of//err is confined to the surface.
}

Switch
Switch in Go is flexible enough to accept an expression that does not have to be limited to shaping or constants. That is, even writing the following statement is possible.
A: = "AAA"
Switch {
Case "AAA" = = A:
Fmt. Println ("Oooooo")
}

Final result output: oooooo


The switch in Go has a bit more to do with the switch in C + + than the one that can be accepted, that is, when a case is matched, it does not continue to match down, which looks like a C + + switch in matching a case followed by a break. If you want switch in go to continue to match down after matching a case, you need to display the plus fallthrough keyword.
Switch I {
Case 0:
Case 1:
F ()
The empty case body
When i = = 0 o'clock, F will not be called!
}
And this:
Switch I {
Case 0:
Case 1:
F ()
Fallthrough
When i = = 0 o'clock, F will be called!
}


Switch Intelligence in C + + accepts shaping, which also makes it not possible to implement a string if you want to use it.
There's no switch in Python.

Cycle
The go Loop has only for, but there are three different forms
1. for Init; Condition post{
...

}

This form is equivalent to the for in C + +

2. For condition {
...
}
This form is equivalent to the while in C + +
3. For {
...
}
This form is equivalent to a dead loop such as while (true) in C + +
The loops in Python have for-in and while two structures. For-in is a loop of a range iteration, and in c++11 there is a range-based for. While words are basically the same.

Continue,break
The continue,break in go can be labeled with a different jump action. In the example below, there is a double loop with the outer loop 5 times and the inner layer looping 5 times.
Func Main () {
Test:
For I: = 0; I < 5; i++ {
For j: = 0; J < 5; J + + {
Fmt. Println (i, J)
If j = = 2 {
Continue Test
}
}
}
}
Result output:
0 0
0 1
0 2
1 0
1 1
1 2
2 0
2 1
2 2
3 0
3 1
3 2
4 0
4 1
4 2

Change the code to
Func Main () {
Test:
For I: = 0; I < 5; i++ {
For j: = 0; J < 5; J + + {
Fmt. Println (i, J)
If j = = 2 {
Break Test
}
}
}
}
Result output:
0 0
0 1
0 2
That is, in go, continue and break can use tags to determine whether they skip the rest of a loop or jump out of a loop (multi-layered nesting), which makes it more flexible to use.

Goto
Well, it's not about Python, though. A goto in Python has been implemented in the form of a library, but this is not within the scope of the discussion and interested in being able to http://entrian.com/goto/

Since break and continue can all be tagged, the go language must be a goto statement. I know that many people have irrational prejudices about goto statements. The goto statement in Go is consistent with the Goto statement in C + +.

We can see that the structure control in Go is more flexible and simpler than C + + and Python.

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.