"go" Go Language learning note four

Source: Internet
Author: User
Tags goto terminates

Process Control

1. Conditional statements

Give me a chestnut:

If x>5 {    return 1;} else{    return 0;}

Attention:

? 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 will Fail:
function ends without a return statement.
The reason for the failure is that the go compiler cannot find the return statement that terminates the Function.

2. SELECT statement

Give me a Chestnut.

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")}

The expression after switch is not even required, such as the following example:

Switch {case    0 <= num && num <= 3:        Fmt. Printf ("0-3") case    4 <= num && num <= 6:        Fmt. Printf ("4-6") case    7 <= num && num <= 9:       Fmt. Printf ("7-9")}    

Attention: 

? 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;
? Contrary to the rules of c, the go language does not need to use a 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.


3. Circular Statements

Loop statements in the go language only support the for keyword, not the while and Do-while Structures. The basic use of the keyword for is very close to C and C + +:

Give me a chestnut:

sum: = 0for I: = 0; I < 10; i++ {    sum + = i}

The go language also takes into account infinite loop scenarios, allowing developers to not write boring for (;;) {} and do {} while (1), but directly simplified to the following wording:

sum: = 0 for {    sum: = 0    if (sum > +) {          break    }    }

Multiple assignments are also supported in a conditional expression, as Follows:

A: = []int{1, 2, 3, 4, 5, 6}for i, j: = 0, len (a) –1; I < j; i, j = i + 1, j–1 {    a[i], a[j] = a[j], a[i]}

Attention:  

? The left curly brace {must be in the same row as for.

? The For loop in the go language, like the C language, allows variables to be defined and initialized in the loop condition, except that the go language does not support multiple assignment statements separated by commas, and multiple variables must be initialized with parallel assignments.

? The For loop of the Go language also supports continue and break to control loops, but it provides a more advanced breaks that can optionally interrupt which loop, as in the following example:


// ...

In this example, the break statement terminates the outer loop at the Jloop label.

4. Jump Statement

The semantics of a goto statement is very simple, which is to jump to a tag within this function, such as:

Func myfunc () {i: = 0 here    :    Fmt. Println (i)    i++    If I < {  goto here   }}

  

  

 

 

"go" Go Language learning note four

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.