This is a creation in Article, where the information may have evolved or changed.
If a: = 1; A > 1 {//define variable A,a only valid in the IF statement
Fmt. Println ("a > 1")
} else {
If a: = 10; A > 1 {
Fmt. Println (a)//result is 10, and C variable scope is consistent
} else {
Fmt. Println (a)
}
}
Three types of For
Func Main () {
Count: = 3
For I: = 0; I < count; i++ {//Classic form
Fmt. Println (i)
}
I: = 0
For I < count {//Like while in C, with terminating conditions only
Fmt. Println (i)
i++
}
i = 0
For {//Dead loop
If I < count {
Fmt. Println (i)
i++
} else {
Break
}
}
}
Switch usage:
Func Main () {
Count: = 1
Switch Count {
Case 1:
Fmt. Println (count)
Fallthrough//Continue checking the next case, shielding the break function
Case 2:
Fmt. Println (count)//case comes with break function
Case 3:
Fmt. Println (count)
Default
Fmt. Println (count)
}
}
Func Main () {
Count: = 1
Switch {//switch does not have a constant, and the If......else If......esle function in C is consistent
Case count = = 1://case need to use logical expressions
Fmt. Println (count)
Fallthrough//Continue checking the next case, shielding the break function
Case Count <= 1:
Fmt. Println (count)//case comes with break function
Case Count >= 1:
Fmt. Println (count)
Default
Fmt. Println (count)
}
}
Break and label
Func Main () {
J:
For j: = 0; J < 5; J + + {
For I: = 0; I < 10; i++ {
If I > 6 {
Break J//Label J is located on the first layer for loop, break J is equivalent to jumping out of the first layer for loop
}
Fmt. Println (i)
}
}
}
Continue and tags
Func Main () {
J:
For j: = 0; J < 5; J + + {
Fmt. Println ("--------")
For I: = 0; I < 10; i++ {
If I > 6 {
Continue J//Tag J is located in the first layer for loop, continue J is equivalent to skipping its following statement and jumping to the first layer for continuation of the first layer loop (the value of the first loop condition J is still incremented)
}
Fmt. Println (i)
}
Fmt. Println ("+++++++")//continue the first layer of the body's language will not be executed, note the difference from C
}
}
Goto and Label
Func Main () {
J:
For j: = 0; J < 5; J + + {
Fmt. Println ("--------")
For I: = 0; I < 10; i++ {
If I > 6 {
Goto J//goto to the first-level for loop, the value of J is re-assigned to 0, at which time the loop is an infinite loop, and the difference between the Continue is: Continue continues the loop, Goto restarts the loop
}
Fmt. Println (i)
}
Fmt. Println ("+++++++")
}
}
When using Goto, be careful to avoid the dead loop, Goto as far as possible to the following code