if else (branched structure)
Go language most commonly used with a flow control if
and for
, and switch
, and goto
primarily to simplify the code, the code decrease repetition born structure, belonging to class flow control expansion
else if
And else
are optional
func ifDemo1() { score := 65 if score >= 90 { fmt.Println("A") } else if score > 75 { fmt.Println("B") } else { fmt.Println("C") } }
Special wording
ifDemo2 FUNC () { if Score: = 65 ; Score> = 90 { // Score belongs inside if local variables , the call will be given out if fmt.Println ( " A " ) } the else if Score> 75 { FMT .Println ( " B " ) } the else { fmt.Println ( " C " ) }
fmt.Println (Score) // undefined: Score }
for (loop structure)
The initial statement -> conditional expression -> loop statement -> End Statement
for initial statement; conditional expression; {end of statement loop statement }
Conditional expression returns the true
body of the loop have been conducting cycles until the condition expression returns false
automatically exit the cycle time.
func forDemo() { for i := 0; i < 10; i++ { fmt.Println(i) } }
The initial statement can be ignored
func forDemo2() { i := 0 for ; i < 10; i++ { fmt.Println(i) } }
The initial statement and end statement can be omitted
func forDemo3() { i := 0 for i < 10 { fmt.Println(i) i++ } }
Infinite loop, you can break
, goto
, return
, panic
statement forces withdraw from circulation.
for { loop statement }
for range
- Arrays, slices, and the index value string is returned.
- map returns keys and values.
- Channel (channel) value in the return channel only.
S: = " Hello Hello " for index, value: = Range S { fmt.Printf ( " % C% D " , index, value) // 0. 1 E 2 L H L. 4. 3 O 8. 5 good you }
--- gentleman at the fact, not at its China; its internal governance, died outer Jang ----