This is the sixth day of my participation in the August More text Challenge. For details, see:August is more challenging

switch

Switch expr {}

// switch season := 1 switch season {case 1: fmt.println (" it's spring ") case 2: fmt.println (" it's summer ") case 3: Case 4: FMT.Println(" it's autumn ") default: case 4: FMT.Println(" it's winter ") FMT.Println(" I don't know what season this is ")} switch season {case 1, 2: FMT.Println(" full of life ") case 3, 4: Default: FMT.Println(" FMT.Println ")}Copy the code

Switch {}

// Score := 80 switch {case score >= 90: fmt.Println(" excellent ") case score >= 80: Case score >= 70: FMT.Println(" good ") default: FMT.Println(" bad ")}Copy the code

for

Method 1

/* for expr1; expr2; Expr3 {} expr1: specifies the initial value of the variable used in the body of the loop. Expr2: specifies the initial value of the loop. Expr3: specifies the initial value of the loop. i < 10; i++ { fmt.Println(i * 10) }Copy the code

Way 2

 /*
 expr1: 一般用于条件判断, 条件不满足, 循环结束
 for expr1 {}
 ​
 一般在for语句外初始化变量
 在for循环体内进行条件的修改
 */
 i := 0
 for i < 10 {
    fmt.Println(float64(i) * (1.5))
    i++
 }
Copy the code

Format 3

For {} */ for {if I == 20 {break} fmt.println (I) I ++} for {} */ for {if I == 20 {break} fmt.println (I) I ++}Copy the code

Form 4

/* arr := []int {1, 2, 3, 4, 5} FMT.Println(arr) // For I, j := 0 len(arr) - 1; i < j; i, j = i + 1, j - 1 { arr[i], arr[j] = arr[j], arr[i] } fmt.Println(arr)Copy the code

ragne

/ * note: */ / * s := []int {1, 3, 5, 7} for I, V := range s {FMT.Println(", I, "); Map m := map[string]int {"shaosiming":18, "dasiming":20} for k, v := range m {FMT.Println("key: ", k, ", value: ", v)} STR := "Hi, gopher" for I, c := range STR {fmt.Println(", I, "); ", string(c)) }Copy the code

if

1 if expr {}

If true {FMT.Println(" you're always right ")}Copy the code

If expr {} else {}

Println := "Println" if c == "Println" if c == "Println" (" Println ") } else {FMT.Println(" let's go to the movie ")}Copy the code

If expr1 {} else if expr2 {} else {}

If weather == "spring" {FMT.Println(" go for a ride ")} else if weather == "summer" {FMT.Println(" go for a ride ")} else if weather == "summer" {FMT.Println(" go for a ride ")} else if weather == "summer" {FMT.Println(" go for a ride ")} else if weather == "summer" {FMT If weather == "fall" {FMT.Println(" go to the swing ")} else if weather == "winter" {FMT.Println(" go skiing ")} else {FMT.Println(" I don't know ") }Copy the code

The use of the break statement

For I := 0; for I := 0; for I := 0; i < 10; Println(" I ", I) for j := 0; j < 10; J ++ {if j == 5 {break} FMT.Println(" j = ", j)}}Copy the code

The use of the continue statement

/* for I := 0; /* for I := 0; i < 10; i++ { for j := 0; j < 10; J++ {/ / don't want to print 3, 5 values if j = = 3 | | j = = 5 {continue} FMT Println (" the inner value of j: ", j)} FMT. Println (" outer I value is: ~ ~ ~ ~ ~ ~ ", I)}Copy the code

Use of labels

Println(" number of times to execute for loop: ", count) for I := 0; i < 5; If I == 3 && count < 5 {count ++ goto TAG} fmt.println (I)} if I == 3 && count < 5 {count ++ goto TAG} fmt.println (I)}Copy the code

conclusion

The switch statement in Go also removes the mandatory use of break, and the case supports multi-value judgment, which is much simpler. The for loop in Go is more flexible, with no explicit formatting restrictions, and is easier to use in combination with Go’s parallel assignment. The range statement in Go is simply not too convenient. We can easily traverse a collection type of data.