Go language conditional statement
conditional statement required by developers to specify one or more conditions, and whether it is true to decide whether to perform the test conditions specified by the statement, and if the condition is false in the implementation of other statements.
The figure below shows the structure of the program language conditional statement:
Statement | description |
---|---|
The if statement | if statement is followed by a boolean expression by the latter or more statements. |
if ... else statement | if statement after can use the optional else statement , else statement in the execution expression is false in Boolean expressions. |
if nested statements | You can if or else if embedded in one or more statements if or else if statement. |
switch statement | switch statement is used to perform different actions based on different conditions. |
select statement | select statement similar to the switch statement, but the execution case will randomly select a run. If there is no case to run, it will block until the case has run. |
if, if ... else statement and python or C-like language
Key to explain:
switch statement based on different conditions to perform different actions, each case branch is unique, individually tested from top to bottom, until the match.
Switch statement executes the process from top to bottom, until a match is found, followed by a match does not need to add break.
switch default case last break statement comes in the case, after a successful match does not perform other case, if we need to do behind the case, you can use fallthrough .
grammar
Go programming language switch statement syntax is as follows:
switch var1 {
case val1:
...
case val2:
...
default:
...
}
It may be any type of var1, while val1 val2 and may be any value of the same type. Or type is not limited to a constant integer, but must be the same type; or end result is the same type of expression.
You can simultaneously test multiple values may meet the criteria, they are separated by commas, for example: case val1, val2, val3.
flow chart:
func tyepe() {
// switch语句练习
grader := "B"
marks := 40
switch marks {
case 90:
grader = "A"
case 80:
grader = "B"
case 50, 60, 70:
grader = "C"
default:
grader = "D"
}
switch {
case grader == "A":
fmt.Println("优秀")
case grader == "B":
fmt.Println("良好")
case grader == "C":
fmt.Println("及格")
case grader == "D":
fmt.Println("不及格")
}
}
func main() {
// traversalstring()
// changeStrings()
tyepe()
}
Type Switch
switch statement may also be used to determine the type-switch interface a variable type variable actually stored.
Type Switch syntax is as follows:
switch x.(type){
case type:
statement(s);
case type:
statement(s);
/* 你可以定义任意个数的case */
default: /* 可选 */
statement(s);
}
func test() {
var x interface{}
switch i := x.(type) {
case nil:
fmt.Println("x的类型是 %T", i)
case int:
fmt.Println("x的类型是 %T", i)
case float32:
fmt.Println("x的类型是 %T", i)
case bool, string:
fmt.Println("x的类型是 %T", i)
default:
fmt.Println("未知类型")
}
}
func main() {
// traversalstring()
// changeStrings()
// tyepe()
test()
}
The above code execution results:
x 的类型 :<nil>
fallthrough
Use fallthrough enforces the back of the case statement, a case under fallthrough not judge whether the result of the expression is true.
package main
import "fmt"
func main() {
switch {
case false:
fmt.Println("1、case 条件语句为 false")
fallthrough
case true:
fmt.Println("2、case 条件语句为 true")
fallthrough
case false:
fmt.Println("3、case 条件语句为 false")
fallthrough
case true:
fmt.Println("4、case 条件语句为 true")
case false:
fmt.Println("5、case 条件语句为 false")
fallthrough
default:
fmt.Println("6、默认 case")
}
}
The above code execution results:
2、case 条件语句为 true
3、case 条件语句为 false
4、case 条件语句为 true
From the results above code output: switch begins at the first expression is a determination of the case to true, a case will continue to operate if the case has fallthrough, program, and it is not to judge the next case expression whether the formula is true.
select statement
Go is a select control structure is similar to the switch statement for communication. Each case must be a communication operation, either transmitting or receiving.
performed randomly select a running case. If there is no case to run, it will block until the case has run. A default clause should always be run.
grammar:
Go programming language select statement syntax is as follows:
select {
case communication clause :
statement(s);
case communication clause :
statement(s);
/* 你可以定义任意数量的 case */
default : /* 可选 */
statement(s);
}
The following describes the syntax of the select statement:
-
Each case must be a communication
-
All channel expression will be evaluated
-
All expressions will be sent to be evaluated
-
If any one of the communication can be carried out, it performs, the other is ignored.
-
If more than one case can run, Select will randomly select a fair execution. Others will not be executed.
otherwise:
- If there is default clause, the statement is executed.
- If there is no default clauses, select the block until a communication can run; Go to channel or not re-value is evaluated.
Examples
application demos select statement:
package main
import "fmt"
func main() {
var c1, c2, c3 chan int
var i1, i2 int
select {
case i1 = <-c1:
fmt.Printf("received ", i1, " from c1\n")
case c2 <- i2:
fmt.Printf("sent ", i2, " to c2\n")
case i3, ok := (<-c3): // same as: i3, ok := <-c3
if ok {
fmt.Printf("received ", i3, " from c3\n")
} else {
fmt.Printf("c3 is closed\n")
}
default:
fmt.Printf("no communication\n")
}
}