"Go language Programming" learning note three

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

Process Control

In the go language, process control is mainly divided into: Select, cycle, jump.
Select: Different operations are performed by different conditions
A loop is a repeating execution.
Jump: Return to an action based on condition
The flow control statements in the go language are:
conditional statements, the corresponding keywords are if, else and else if;
Select the statement, the corresponding keyword is switch, case and select (will be described in the introduction of the channel);
loop statements, corresponding keywords for and range;
Jump statement, the corresponding keyword is goto.
Similarly, there are keywords like break, continue, and Fallthrough in the go language.

Conditional statements:

ifa5 {return0else {return1}

Conditional statements do not need parentheses to enclose conditions ();
The curly braces {} must exist regardless of the statement body.
The left curly brace {must be in the same row as if or else;
After the IF, the conditional statement can be added before the variable initialization statement, use; interval;
In a function that has a return value, the "final" return statement is not allowed to be included in the If...else ... Structure, otherwise the compilation fails:

SELECT statement

switch  I { case   0 : FMT. Printf ( "0" ) case   1 : FMT. Printf ( "1" ) case   2 : fallthrough  case   3 : FMT. Printf ( "3" ) case   4 ,  5 ,  6 : FMT. Printf ( "4, 5, 6" ) default : FMT. Printf ( "Default" )}  

Left Curly brace {must be in the same row as switch;
A conditional expression is not limited to a constant or an integer;
Multiple result options can appear in a single case;
The go language does not need to use break to explicitly exit a case;
The next case will be executed only if the Fallthrough keyword is explicitly added to the case;
You can not set the conditional expression after switch, in which case the entire switch structure with multiple if...else ... The logical function of the same.

Looping statements

0fori0i10i{sum += i}

The loop statement in the go language only supports the FOR keyword, not the while and Do-while
The left curly brace {must be in the same row as for.
The go language does not support multiple assignment statements separated by commas, and multiple variables must be initialized with a parallel assignment.
Provides a more advanced break that can optionally interrupt which loop

for05; j++ {for010; i++ {if5 {break...

Jump statement

funcmyfunc{i := 0HERE:fmt.Println(i)i++if i < 10 {goto HERE}}
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.