[Go]---control structure

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

Email: hahayacoder@gmail.com

1 common control structures in the go language are: If statements, switch statements, and for loops

2 If statements are similar to the C language, but the condition is not enclosed in parentheses, it must have curly braces.

//if

varvalue int = 2

ifvalue%2 = = 0 {

FMT. Printf ("%d is even \ n", value)

    }

//if...else

ifvalue%2 = = 0 {

FMT. Printf ("%d is even \ n", value)

} else{

FMT. Printf ("%d is odd \ n", value)

    }

//if Multi-branch

Ifvalue < 0 {

FMT. Printf ("%d is negative \ n", value)

} Elseif value = = 0 {

FMT. Printf ("%d is 0 \ n", value)

} else{

FMT. Printf ("%d is positive \ n", value)

}

2 switch statement does not break, separate multiple case values with commas

/**switch branch statement Switch Statement No break can use a comma case multiple values ***/

varnumber int = 2

Switchnumber {

case1:

FMT. Println ("one")

case2:

FMT. Println ("both")

case3, 4,5:

FMT. Println ("Three,four,Five")

Default:

FMT. PRINTLN ("Invalidvalue")

}

3 For statement is also similar to C language, just to judge the condition does not use parentheses

/*** for loop ***/

for I: = 1; I < ten; i++ {

FMT. Printf ("%d\t", i)

    }

4 The Go language does not have a while statement

5 Small examples

Package Mainimport "FMT" Func Main () {/***IF statement if statement has no parentheses but must have curly braces to note the position of the curly braces ***///ifvar value int = 2if value%2 = = 0 {fmt. Printf ("%d is even \ n", value)}//if...elseif value%2 = = 0 {fmt. Printf ("%d is even \ n", value)} else {fmt. Printf ("%d is odd \ n", value)}//if Multi-branch if value < 0 {fmt. Printf ("%d is negative \ n", value)} else if value = = 0 {fmt. Printf ("%d is 0 \ n", value)} else {fmt. Printf ("%d is positive \ n", value)}/*** for loop ***/for i: = 1; I < 10; i++ {fmt. Printf ("%d\t", i)}fmt. Printf ("\ n")/** Switch Branch statement switch statement does not break can use comma case multiple values ***/var number int = 2switch # {case 1:fmt. Println ("one") case 2:fmt. Println ("a") Case 3, 4, 5:fmt. Println ("Three, four, five") default:fmt. PRINTLN ("Invalid value")}/*** the idea that there is no while loop in the Go Language ***/}


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.