main content
- 4.1 conditional --if
- 4.2 build cycle --for
- 4.3 Example: multiplication table
- 4.4 circulating key (for range) - direct access to the data objects and the index
- 4.5 branch selection (switch) - Analyzing the conditional branch has a plurality of
- 4.6 Code jump to the specified label (GOTO)
- 4.7 out of designated cycle (break)
- 4.8 out of current through the bad (continue)
Go语言规定与if匹配的左括号“{” 必须与if 和表达式放在同一行
, If you try to "{" any other location, will trigger a compile error
与else 匹配的 “{” 也必须与else 在同一行
else也必须与上-个if 或else if 的右边的大括号在一行
format:
{1 if expression branch 1 } {2 expression if the else branch 2 } {the else branch 3 } //> 10
Special wording
if there is specific wording, if the expression may be added before an execution statement, then the value is determined according to the variable, such as:
if err := Connect(); err != nil { fmt.Println(err) return }
Connect () function is assumed, err: = Connect () can be seen as an expression
4.2 build cycle --for
Go language in all types of cycles are completed can be used for keyword
for initial statement; conditional expression; end {statement loop Code }
for loop through break
, goto
, return
, panic
statement forces withdraw from circulation
4.2.1 for initial statement - the statement begins execution
The initial statement can be ignored, but the semicolon after the initial statement must write
step := 2 for ; step > 0; step-- { fmt.Println(step) } // 2 // 1
4.2.2 for Conditional Expressions - controlling whether the cycle switch
The criteria expression can also be ignored, ignored the default is an infinite loop
-
Infinite loop with an executable statement at the end of cycle
var i int for ; ; i++ { if i > 10 { fmt.Println("i > 10") break; } } // i > 10
-
Infinite loop
for { if i > 10 { fmt.Println("i > 10") break } i++ } // i > 10
4.2.3 for the end of the statement - at the end of each execution of the loop statement
In statements before the end of each cycle enforced by the break, goto, return, panic and other statement forces launched
Example:
// iterate multiplication table for X: =. 1; X <10; ++ {X for Y: =. 1; Y <= X; Y ++ { SUM: = X * Y fmt.Printf ( "% D% = D * D% ", X, Y, SUM) } fmt.Printf (" \ R & lt \ n-") } results 1 1 = 1 * 2 * 2 * 1 = 2 2 = 4 3 * 2 * 1 = 33 = 6 3 * 3 = 9 4 * 1 = 44 * 2 = 84 * 3 = 124 * 4 = 16 5 * 1 = 55 * 2 = 105 * 3 = 155 * 4 = 205 * 5 = 25 6 * 1 = 66 * 2 = 126 * 3 = 186 * 4 = 246 * 5 = 30 6 * 6 = 36 7 * 1 = 77 * 2 = 147 * 3 = 217 * 4 = 28 7 * 5 = 357 * 6 = 42 7 * 7 = 49 8 * 1 = 88 * 2 = 168 * 3 = 248 * 4 = 328 * 5 = 408 * 6 = 488 * 7 = 56 8 * 8 = 64 9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81
4.4 circulating key (for range) - direct access to the data objects and the index
Go language may be used for range traversal, data, slice, string, map and a channel (channel)
law:
-
Arrays, slices, returns the index string
-
return key and value map
-
Channel (channel) returns only values within the channel
4.4.1 through the array, slice - and get indexed element
for key, value := range []int{1,2,3,4} { fmt.Printf("key: %d, value: %d\r\n", key, value ) } //key: 0, value: 1 //key: 1, value: 2 //key: 2, value: 3 //key: 3, value: 4
4.4.2 Traversal string - get character
for key, value := range "abcdef" { fmt.Printf("key: %d, value: 0x%x\r\n", key, value ) } //key: 0, value: 0x61 //key: 1, value: 0x62 //key: 2, value: 0x63 //key: 3, value: 0x64 //key: 4, value: 0x65 //key: 5, value: 0x66
4.4.3 traversing dictionary map-- get the keys and values
m := map[int]string{ 1 : "one", 2 : "two", 3 : "three", } for key,value := range m { fmt.Printf("key: %d, value: %s\r\n", key, value ) } // key: 1, value: one // key: 2, value: two // key: 3, value: three
4.4.3 Channel traverse channel - receiving channel data
C: = the make (Chan int) // goroutines a start, push data to the channel 2, 3, and close the channel Go FUNC () { C <-. 1 C <- 2 C <-. 3 Close (C) } () for V: Range = C { fmt.Println (V) } //. 1 // 2 //. 3
4.4.5 traversal select only the required variables
No need to obtain the key or value, may be used 下划线“-”
instead of, i.e.,匿名变量
Anonymous variable, understood as a placeholder itself will not be space allocation, it will not take up the variable name
for _, value := range []int{1,2,3,4} { fmt.Printf("value: %d\r\n", value ) } // value: 1 // value: 2 // value: 3 // value: 4
4.4.6 for summary
for features:
-
Go for language contains the initial conditions, conditional expressions, the end of the statement, the three can be omitted
-
Support for range arrays, slices, string, map, traverse channel
-
Variables can be used to obtain anonymous variable only needs
4.5 branch selection (switch) - Analyzing the conditional branch has a plurality of
4.5.1 The basic wording
Each switch between the case and the case is independent of the code block, code block case does not require out of the current through the break statement to the next line to avoid
Go Language Requirements Each switch can have only one default branch
var a = "hello" switch a { case "hello": fmt.Println(1) case "world": fmt.Println(2) default: fmt.Println(0) }
Branch common scenarios:
-
A multi-valued branch
switch a { case "hello", "world" : fmt.Println("Hi") }
-
Expressions branch
var r int = 11 var s int = 5 switch { case r > 10 && r <20: fmt.Println(r) case s > 2 && s <10: fmt.Println(s) } // 11
If only one variable to determine when, behind the switch can not add a variable r
4.5.2 across the case of fallthrough - compatible design c language case
fallthrough
Keyword is used to implement the inter-case execution branches should Go case language code block are independent
str := "hello" switch { case str == "hello": fmt.Println("hello") fallthrough case str != "world": fmt.Println("world") } // hello // world
4.6 Code jump to the specified label (GOTO)
goto statement unconditional jump between the code through the label, you can quickly exit out of the loop on avoiding duplication of some help
4.6.1 Using a multi-layer loop goto exit
Original code:
packeage main import ("fmt") func main() { var breakAgain bool for x := 0; x < 10; x++ { for y := 0; y <10; y++ { if y == 2 { breakAgain = true break } } if breakAgain { break } } fmt.Println("done") }
To optimize the use of the goto statement:
After using the goto statement, no additional variables can be quickly exit the loop all
main PACKEAGE again Import ( "FMT") FUNC main () { for X: = 0; X <10; ++ {X for Y: = 0; Y <10; ++ {Y IF Y == {2 // jump to label goto breakHere } } } // manual return, execution proceeds to avoid label return // tags breakHere: fmt.Println ( "DONE") }
4.6.2 Unified error handling
err := firstCheckError() if err != nil { goto onExit } err = secondCheckError() if err != nil { goto onExit } fmt.Println("done") return onExit: fmt.Println(err)
4.7 out of designated cycle (break)
statement can break out for, switch and block select
break statement add tags later, represents an exit code block corresponding to the label, the label must be defined on the corresponding code block for, switch and select the
main PACKEAGE again Import ( "FMT") FUNC main () { Loop: for I: =. 1; I <=. 3; I ++ { for J: = 0; J <. 5; J ++ { Switch J { Case 2: fmt.Println (I, J) // exit the bis-circulating loop under the code label BREAK loop case. 3: fmt.Println (I, J) BREAK loop } } } }
4.8 out of current through the bad (continue)
continue statement ends the current through the bad, to begin the next cycle is limited to use within a for loop
When adding back the label continue statement indicating the start of the cycle corresponding to the tag
packeage main import ("fmt") func main() { loop: for i:=0; i<=3; i++ { for j := 0; j<5;j++ { switch j { case 2: fmt.Println(i,j) continue loop } } } // 0 2 // 1 2 // 2 2 // 3 2 }