Go Basic statement

Source: Internet
Author: User

Increment Decrement statement
in Go, + + and--are used as statements rather than as expressions

Package Mainimport   " FMT " Func Main () {        A:=1            //a=a++   // statement rather than expression, Only left, a++ FMT is not supported on Go                . Println (a)  }

Judgment statement if
The conditional expression has no parentheses, and the block needs to be curly-braces even if there is only one row
Supports one (only 1) initialization expressions (can be in parallel mode)
The opening brace must be on the same line as the conditional statement or else
Support single-line mode
The variable in the initialization statement is the block level, while the external variable with the same name is hidden

Package Mainimport   " FMT " Func Main () {        A:=ten        if a,b:=2,3; a<b {           //  The left large expansion number must be accompanied  by if a:=2,b:=3; a<b syntax error, only supports 1 initialization expression                fmt. Println (a)  //        }         //    only in the IF statement block has the valueof        FMT. Println (b)  //}

Loop statement for
Go is only for a loop statement keyword, but it supports 3 different forms
Initialization and stepping expressions can be multiple values
The conditional statement is re-examined each time, so it is not recommended to use a function in a conditional statement, try to calculate the condition in advance and replace it with a variable or constant
The left brace must be on the same line as the conditional statement (formatted)
The first form of

Package Mainimport   " FMT " Func Main () {     A:=0for     {        a+ +        if a>10  {                break        }        fmt. Println (a)             }   }

The second form of

Func Main () {     A:=0      for a< + + +                fmt. Println (a)     }   }

The third form of

Func Main () {     for a:=0;a<; a++ {        fmt. Println (a)     }   }

Attention

 func Main () {s:  = " ABCD   for  a:=0 ; A<len (s); a++{ every loop executes Len (s) It is recommended that you assign the variable to the   FMT in the for-judgment expression. Println (A)}}  

Branch SELECT switch statement
You can use any type or expression as a conditional statement
No write break is required, once the condition meets automatic termination
If you want to continue with the next case, you need to use the Fallthrough statement
supports an initialization expression (which can be parallel), with a semicolon on the right-hand side
The left brace must be on the same line as the conditional statement

Func Main () {a:=1        Switcha{ Case 0: FMT. Println ("a=0")         Case 1: FMT. Println ("a=1") fmt. Println ("OK")        default: FMT. Println ("default")        } }

Func Main () {a:=1        Switch {         Casea>=0: FMT. Println ("a=0")         Casea>=1: FMT. Println ("a=1") fmt. Println ("OK")        default: FMT. Println ("default")        } }//output a=0 auto break only
Func Main () {a:=1        Switch {         Casea>=0: FMT. Println ("a=0") Fallthrough Casea>=1: FMT. Println ("a=1") fmt. Println ("OK") Fallthroughdefault: FMT. Println ("default")        } }/*Output A=0a=1okdefault*/
Func Main () {Switcha:=1; {         Casea>=0: FMT. Println ("a=0") Fallthrough Casea>=1: FMT. Println ("a=1") fmt. Println ("OK") Fallthroughdefault: FMT. Println ("default")} FMT. Println (a);//undefined:a}//output Results same as above

Jump Statement Goto,break,continue
Three syntax can be used in conjunction with a label
Label names are case-sensitive and can cause compilation errors if not used
Break and continue mate tags can be used to jump out of multi-layer loops
Goto is the adjustment of the execution position

Func Main () {  //  LABEL1:for  {    for i:=0; i<; i++{      fmt. Println (i);       if i>3{        // break LABEL1        goto  LABEL2      }}}  LABEL2:}

 func Main () {LABEL1:  for  I:=0 ; I<{ for   { continue   LABEL1 FMT.    Println (i); } FMT.  Println (i); } FMT. Println (  ok!   " )}  //  output OK only !
Func Main () {  LABEL1:  for i:=0;i<, i++{    for  {      FMT. Println (i)      continue  LABEL1    }}  }// output 0-9//  continue converted to Goto will die loop output 0

Go Basic statement

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.