Go language study essay 2019-10-30
1. Conditional statements
1)if
if a > 10{
//do something
}
2)if-else
if a > 10{
//do something
} else{
//do other things
}
Notice
else statements should be on the same line as the if statement’s ‘}’,otherwise an error will be reported.
3)nested if
if a > 10{
//do something
if b < 10{
//do something
}
}
4)switch
switch a{
case 1:
...
case 2:
...
default:
...
}
Notice
You don’t need to write break statement after case statement.
If you want to continue excute the code which behind matched case statements,please use fallthrough.
5)select
Select statement is like switch statement,but the different is that switch statement’s every case statement is a channel operation.Otherwise,select statement hasn’t conditional experession,his excution is random,for example:
var c1,c2 chan int
var a,b int
switch{
case c1 <- a:
//send
case b = <-c2:
//receive
default:
//blabla...
}
Notice
Go doesn’t have trinocular operator,so it doesn’t support the conditional judgment of the ?: form.
2.Loop statement
1)for loop
for i :=0; i < 10; i++{
//do someting
}
for a < b{
//do someting
}
for index,item := range numbers{
//do someting
}
2)nested loop
for i :=0; i < 10; i++{
for j :=i; j < 10; j++{
//do something
}
}
3)break,continue,goto
- break
for i :=0; i < 10; i++{
if a>5 {
// if a>5 ,stop loop
break
}
}
- continue
for i :=0; i < 10; i++{
if a == 5 {
// skip this loop
i++
continue
}
}
- goto
LOOP: fmt.Println("lalala")
for i :=0; i < 10; i++{
if a == 5 {
// stop loop and jump to line 1
goto LOOP
}
}
Notice
forever loop:
for true{
//I love you
}