Process control of Go language foundation

Keywords: Go

1. If else (branch structure)

1.1 if judgment basic syntax

The format of if condition judgment is as follows:
if expression 1{
    Branch 1
 }else if expression 2{
    Branch 2
} else{
    Branch 3
}

The Go language specifies that the left parenthesis {that matches the if must be placed on the same line as the if and expression, and {that is placed elsewhere will trigger a compilation error.
Similarly, {matching else must be written on the same line as else, and else must be on the same line as the brace to the right of the previous if or else if.
package main

import "fmt"

func ifDemo1()  {
    score := 65
    if score>=90{
        fmt.Println("A")
    }else if score>75{
        fmt.Println("B")
    }else{
        fmt.Println("C")
    }
}
func main() {
    ifDemo1()
}

//Result:
C

1.2 special writing method of if condition judgment

if There is also a special way to write conditional judgment if Add an execution statement before the expression, and then judge according to the variable value
package main

import "fmt"

func ifDemo2()  {
    if score := 65;score>=90{
        fmt.Println("A")
    }else if score>75{
        fmt.Println("B")
    }else{
        fmt.Println("C")
    }
}
func main() {
    ifDemo2()
}

//Result:
C

2.for loop structure

2.1 basic syntax of for loop

All loop types in the GO language can be done using the for keyword.

The basic format of the for loop is:
for initial statement; conditional expression; end statement{
    Loop body statement
}

func forDemo() {
    for i := 0; i < 10; i++ {
        fmt.Println(i)
    }
}

2.2 for loop omits initial statement

for The initial statement of the loop can be ignored, but the semicolon after the initial statement must be written, for example:

package main

import "fmt"

func forDemo2()  {
    i:=0
    for ;i<10;i++{
        fmt.Println(i)
    }
}
func main() {
    forDemo2()
}

//Result:
0
1
2
3
4
5
6
7
8
9

2.3 omit the initial statement and the end statement

for Both the initial and end statements of the loop can be omitted
//This way of writing is similar to while in other languages. A conditional expression is added after while. When the conditional expression is satisfied, the loop continues, otherwise the loop ends.

package main

import "fmt"

func forDemo3()  {
    i:=0
    for i<5{
        fmt.Println(i)
        i++
    }
}
func main() {
    forDemo3()
}

//Result:
0
1
2
3
4

2.4 infinite cycle

for {
    Loop body statement
}
for loop can be forced to exit the loop through break, goto, return and panic statements.

2.5 for range

GO language can use for range to traverse array, slice, string, map and channel. The return value traversed by for range has the following rules:
1. Array, slice, string return index and value.
2.map returns the key and value.
3. Channel only returns the value in the channel.

3.switch case

Use switch Statement can easily judge a large number of values.

func switchDemo1() {
    finger := 3
    switch finger {
    case 1:
        fmt.Println("Thumb")
    case 2:
        fmt.Println("index finger")
    case 3:
        fmt.Println("middle finger")
    case 4:
        fmt.Println("Ring finger")
    case 5:
        fmt.Println("Little finger")
    default:
        fmt.Println("Invalid input!")
    }
}
Go Language for each switch Only one default Branch.
//A branch can have multiple values, and multiple case values are separated by commas.
func testSwitch3() {
    switch n := 7; n {
    case 1, 3, 5, 7, 9:
        fmt.Println("Odd number")
    case 2, 4, 6, 8:
        fmt.Println("Even numbers")
    default:
        fmt.Println(n)
    }
}
Branches can also use expressions, when switch Statement does not need to be followed by a judgment variable.
func switchDemo4() {
    age := 30
    switch {
    case age < 25:
        fmt.Println("Study hard")
    case age > 25 && age < 35:
        fmt.Println("Work hard")
    case age > 60:
        fmt.Println("Enjoy it")
    default:
        fmt.Println("It's good to be alive.")
    }
}
fallthrough Syntax can execute case Next caseļ¼ŒFor compatibility C In language case Designed.
func switchDemo5() {
    s := "a"
    switch {
    case s == "a":
        fmt.Println("a")
        fallthrough
    case s == "b":
        fmt.Println("b")
    case s == "c":
        fmt.Println("c")
    default:
        fmt.Println("...")
    }
}

//Result:
a
b

4.break

A break statement can end blocks of code for, switch, and select.
The break statement can add a tag after the statement, which means to exit the code block corresponding to a tag. The tag must be defined on the code block corresponding to for, switch and select.
package main

import "fmt"

func breakDemo1(){
    BREAKDEMO1:
        for i:=0;i<10;i++{
            for j:=0;j<10;j++{
                if j==2{
                    break BREAKDEMO1
                }
                fmt.Printf("%v-%v\n",i,j)
            }
        }
}
func main() {
    breakDemo1()
}

//Result:
0-0
0-1

5. Continue

continue Statement can end the current loop and start the next loop iteration process, only in for Use in cycle.
//When a tag is added to continue, it indicates that the loop corresponding to the starting tag

package main

import "fmt"

func continueDemo1(){
    CONTINUEEMO1:
        for i:=0;i<10;i++{
            for j:=0;j<10;j++{
                if j==2{
                    continue CONTINUEEMO1
                }
                fmt.Printf("%v-%v\n",i,j)
            }
        }
}
func main() {
    continueDemo1()
}

//Result:
0-0
0-1
1-0
1-1
2-0
2-1
3-0
3-1
4-0
4-1
5-0
5-1
6-0
6-1
7-0
7-1
8-0
8-1
9-0
9-1

Posted by shutat on Wed, 11 Dec 2019 00:48:00 -0800