Process control language go mainly if, for and switch.
if else (branched structure)
if go language of judgment:
func main() { score:=65 if score>=90{ fmt.Println("A") }else if score>=80{ fmt.Println("B") }else{ fmt.Println("C") } }
Need two things to note:
1. The expression can not be used ()
2. Starting braces must immediately behind the expression, not a separate line
In addition, if there is another way of writing is determined:
func main() { if score:=65; score>=90{ fmt.Println("A") }else if score>=80{ fmt.Println("B") }else{ fmt.Println("C") }
fmt.Println(score) // 报错,获取不到score }
The wording of the definitions of the variables is written in an expression, and wrote the role of the variables defined in the local.
for (loop structure)
The for loop structure similar to other languages:
for initial statement; conditional expression; end {statement loop statement }
requires attention:
1. for not adding back ()
2. As if the braces and, not on a separate line
example:
func main() { for i:=0;i<10;i++{ fmt.Println(i) } }
for loop initial statement can be ignored, but the semicolon after the initial statement must be written, for example:
func main() { i:=0 for ;i<10;i++{ fmt.Println(i) } }
for loop statement and the initial end statement can be omitted, for example:
func main() { i:=0 for i<10{ fmt.Println(i) i++ } }
switch case
Basic usage:
main FUNC () { Week: =. 3 Switch Week { Case 0 : FMT .Println ( "Sun") Case . 1 : FMT .Println ( "Monday") Case 2 : FMT .Println ( "Tuesday") Case . 3 : FMT .Println ( "Wednesday") Case 4 : fmt .Println ( "Thursday") Case 5 : fmt .Println ( "Friday") default: fmt .Println ( "Saturday") } }
Each switch can only have one default
A branch may have multiple values, a plurality of intermediate values separated by a comma case:
main FUNC () { NUM: =. 1 Switch {NUM Case 1,3,5,7,9 : FMT .Println ( "It is an odd number") Case 2,4,6,8,0 : FMT .Println ( "this is an even number ") default: FMT .Println (NUM) } }
The switch can also be assigned on the inside:
main FUNC () { Switch NUM: =. 1; NUM { Case 1,3,5,7,9 : FMT .Println ( "It is an odd number") Case 2,4,6,8,0 : FMT .Println ( " this is even ") default: FMT .Println (NUM) } }
You can also use expressions branch, this time behind the switch statement does not need to talk to determine variables. E.g:
func main() { score:=90 switch { case score>=90: fmt.Println("优秀") case score>=80: fmt.Println("良好") case score>=60: fmt.Println("及格") } }
fallthrough next case can be performed in case of satisfying the condition:
func main() { b:=true switch b { case true: fmt.Println(1) fallthrough case false: fmt.Println(2) } }
As above, only printing without fallthrough 1, 1 and 2 together with the printing fallthrough