This is a creation in Article, where the information may have evolved or changed.
The difference between Go's If,for,switch and C
People who have a C foundation know that if, for, and switch conditional languages are bracketed. Basic format:
if{}for(语句1;语句2;语句3{}switch(语句){}
In go, these grammatical rules and C still have a difference, summed up as a sentence is to remove the parentheses. The syntax rules are as follows:
if 语句 {}for 语句1;语句2;语句3 {}//跟C一样,这三个语句可以省掉任何一个语句switch 语句 {}
The difference is actually very simple. Due to the absence of parentheses, they have more functionality with C.
If
If there is a strong place to declare a variable in the conditional judgment statement, the scope of the variable can only be within the logical block of the condition, and other places will not work.
if x:=getXdata(); x>10 {//x的作用域只在这个括号里有作用,除了括号就不起作用了。}
For
The C language has a while, Do-while Loop statement. Go is not, how to achieve it. See below:
sum:=1for ;sum<100;{}或者省略;forsum<100 {}
Haha, it's easy.
Goto
Same as the grammar rules of C.
func testFunc(){ ...Here: //以冒号结束作为标签 ... goto Here//跳转到Here处