Golang Learning Notes 4--control statements

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

1. Basic knowledge Supplement (pointer, increment decrement statement)

Pointer

Go retains pointers, but unlike other programming languages, pointer operations and the "-to" operator are not supported in go, and direct use of the "." Selector to manipulate the members of the 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

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

1a++//但不能这样:a := a++,只能作为单独的语句独占一行

2.if Judgment Statement

    • conditional expression without parentheses

    • Supports an initialization expression (which 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

func main(){  10  if a:=1 {      fmt.Println(a)  } fmt.Println(a) }//输出为:  1     10

3.for Loop statement

    • Go is only for a loop statement keyword, but it supports 3 different forms
//第一种(同其他语言的while true {}):func main() {  1  for {      a++      if3{          break      }  }  fmt.Println(a)}//第二种(同其他语言的:while a <= 3 {}):  func main() {  1  for3 {     a++  }  fmt.Println(a)}//第三种:func main() {  1  for03; i++ {     a++  }  fmt.Println(a)}
    • Initialization and stepping expressions can be multiple values

4.switch Selection Statement

There is no need to manually write a break for each case in the go language to prevent the program from continuing to match down. If you want to achieve the purpose of continuing the match, you can use the keyword: fallthrough

funcMain () {A: =1  SwitchA Case 0: FMT. Println ("A=0") Case 1: FMT. Println ("A=1")} FMT. Println (A)}funcMain () {A: =1  Switch{ CaseA >=0: FMT. Println ("A=0")Fallthrough       CaseA >=1: FMT. Println ("A=1")} FMT. Println (A)}//At this time the scope of a is only within the switch expressionfuncMain () {SwitchA: =1{ CaseA >=0: FMT. Println ("A=0")Fallthrough       CaseA >=1: FMT. Println ("A=1")} FMT. Println (A)}

5. Jump statements

Break, continue, goto mate tags using

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.