Golang basis operators and control statements _02-

Precautions

Operators

tips

  • Go left to right of the operator is bound
  • Highest to lowest priority:
    Write pictures described here

Shift operation

a << 10
//a*(2^10)
//右移类似

Bitwise Operators

/*
6 : 0110
11: 1011


----------


&   0010
|   1111
^   1101
&^  0100 
//第四个运算符是,第二行这一位如果是1,就上0,如果是0,就上第一行该位置的数
*/

Operators && and ||

  • && if the first one does not comply, it will not compute the second expression
  • II Similarly, if the first in line, it will not compute the second expression

pointer

  • Go in, while retaining a pointer, but does not support pointer arithmetic, and "->" operator, to operate the direct use of pointers audience members. ""
  • Operator "&" to the variable address, "*" is accessed indirectly through a pointer audiences
  • The default value is nil , not NULL
    Ascending Descending statements
  • In Go, + and - as a statement rather than an expression
  • Expression is on the right side of the equal sign, the statement can not be
  • And --a is not possible, a-- job

Control statements

If the judge sentences

  • Without parentheses, a space on the line
  • Add a local variable may be initialized, only in the domain if statement
    if a:=1 ; a>0 { }
  • The opening brace must be on the same line if statement

For loop

  • Only for a loop keyword, but three forms of support
  • To the left and braces for keywords on the same line
  • And initialization step may be a plurality of expression values
  • Each conditional statement will be checked, it is not recommended to use functions in a conditional statement, try to calculate in advance the condition, or the vector and replaced with a variable
func main(){
    a := 1
    for {
        a++
        if a>3 {
            break;
        }
    }
//do...while型
}
func main(){
    a := 1
    for a<=3 {
        a++
    }
//while型
}
func main(){
    a := 1
    b := "string"
    l := len(b)
    for i := 0; i<l ; i++{
        a++
    }
//for型
}

Select statement switch

  • Opening brace must switch in a row
  • The back of the case can be any type of conditional statement or expression
  • No need to write break, once they meet the conditions will automatically terminate if you want to proceed to the next case, you need to add fallthrough statement
  • Behind the switch can add an initialization expression, to add a semicolon, if only a simple variable name, do not add a semicolon
func main(){
    a := 1
    switch a {
    case 0:
        fmt.Println("a=0")
    case 1:
        fmt.Println("a=1")
    default:
        fmt.Println("nothing")
    }
    fmt.Println(a)
}
//a=1
//1
func main(){
    a := 1
    switch {
    case a>=0:
        fmt.Println("a=0")
        fallthrough
    case a>=1:
        fmt.Println("a=1")
    default:
        fmt.Println("nothing")
    }
    fmt.Println(a)
}
/*
a=0
a=1
1
*/
func main(){
    a := 6
    switch a := 0; {
    case a>=0:
        fmt.Println("a=",a)
        a++
        fallthrough
    case a>=1:
        fmt.Println("a=",a)
    default:
        fmt.Println("nothing")
    }
    fmt.Println(a)
}
/*
a= 0
a= 1
6
*/

Jump statement goto, break, continue

  • Three syntax can use with the label
  • Label names are case sensitive , if use can cause compile errors
  • The label may break and continue for a multilayer bounce cycle, if there is no label, is the normal role
  • goto is to adjust the position of the Executive, with the label and the other two are not the same statement results
func main(){
LABEL1:
    for {
        for i:=0 ; i< 10 ;i++ {
            if i>3 {
                break LABEL1
                //continue LABEL1  死循环
                //goto LABEL1  重新执行这个大循环
            }
        }
    }
}

Guess you like

Origin www.cnblogs.com/leafs99/p/golang_basic_02.html