"Go Learning Notes" 5, control statements

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

Pointer

Go retains pointers, but unlike other programming languages, pointer arithmetic and the "-to" operator are not supported in go, but directly with "." Selectors to manipulate the members of the pointer target object

    • Operator "&" takes the variable address and uses "*" to indirectly access the target object using the pointer
    • The default value is nil and not null

Increment Decrement statement

In go, + + and – are used as statements rather than as expressions.

SELECT statement Switch

    • 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 in parallel), and the right side needs to be well-divided
    • The left brace must be on the same line as the conditional statement
12345678910111213141516171819202122232425262728293031323334353637
 func main() {A: =1SwitchA Case 0: FMT. Println ("A=0") Case 1: FMT. Println ("A=1")}fmt. Println (A)} func main() {A: =1Switch{ CaseA >=0: FMT. Println ("A=0")Fallthrough CaseA >=1: FMT. Println ("A=1")}fmt. Println (A)} func main() {SwitchA: =1; { CaseA >=0: FMT. Println ("A=0")FallthroughCAEs a >=1: FMT. Println ("A=1")}}

Jump Statement goto Break Continue

    • Three statements can be used in conjunction with labels using the
    • tag name to be case-sensitive, which would cause a compilation error if not used
    • break and continue mate tags can be used to jump out of a multi-layered loop
    • goto is the adjustment execution position, The result of matching labels with the other 2 statements is not the same
 1234567891011121314151617181920212223 242526 
 func  main   ()   {label:for  {for  i : = 1 , I < 10 ; i++ {if  i > Span class= "number" >2  {break  label}else  {fmt. Println (i)}}}}func  main   ()   {label:for  i: = 0 ; i < Span class= "number" >10 ; i++ {for  {fmt. Println (i) continue  LABEL}} 

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.