目录
条件语句需要开发者通过指定一个或多个条件,并通过测试条件是否为 true 来决定是否执行指定语句,并在条件为 false 的情况在执行另外的语句。
Go 语言提供了以下几种条件判断语句:
语句 | 描述 |
---|---|
if 语句 | if 语句由一个布尔表达式后紧跟一个或多个语句组成。 |
if…else 语句 | if 语句后可以使用可选的 else 语句, else 语句中的表达式在布尔表达式为 false 时执行。 |
if 嵌套语句 | 你可以在 if 或 else if 语句中嵌入一个或多个 if 或 else if 语句。 |
switch 语句 | switch 语句用于基于不同条件执行不同动作。 |
select 语句 | select 语句类似于 switch 语句,但是select会随机执行一个可运行的case。如果没有case可运行,它将阻塞,直到有case可运行。 |
1、if 条件语句
if 语句由布尔表达式后紧跟一个或多个语句组成。
Go 编程语言中 if 语句的语法如下:
if 布尔表达式 {
/*在布尔表达式为true时执行*/
}
//考试成绩及格,送笔记本,并且输出考试成绩
package main
import "fmt"
//成绩不得低于60分
func main() {
var source = 80
if source >= 60 {
fmt.Println("赠送笔记本一个!")
}
fmt.Println("考试成绩为:", source)
}
//输出结果如下
赠送笔记本一个!
考试成绩为: 80
2、if…else 语句
if 语句后可以使用可选的 else 语句,else 语句中的表达式在布尔表达式为 false 时执行
Go 编程语言中 if…else 语句的语法如下:
if 布尔表达式 {
/*在布尔表达式为true时执行*/
}else {
/*在布尔表达式为false时执行*/
}
//考试成绩高于60分为及格,赠送笔记本;考试成绩低于60不及格,罚抄;且输出考试成绩
package main
import "fmt"
func main() {
var source = 98
if source >= 60 {
fmt.Println("赠送笔记本!")
} else {
fmt.Println("罚抄!")
}
fmt.Println("考试成绩为:", source)
}
//输出结果如下
赠送笔记本!
考试成绩为: 98
3、if 语句嵌套
可以在 if 或 else if 语句中嵌入一个或多个 if 或 else if 语句
if…else 语句的语法如下:
if 布尔表达式1 {
/*在布尔表达式 1 为true时执行*/
if 布尔表达式2 {
/*在布尔表达式 2 为true时执行*/
}
}
//学校举办田径比赛,10秒内进入决赛;进决赛后根据性别分男女组
package main
import "fmt"
func main() {
var (
sec = 8.8
sex = "boy"
)
if sec < 10 {
fmt.Println("恭喜进入决赛!")
if sex == "boy" {
fmt.Println("您进入的是男子组")
} else {
fmt.Println("您进入的是女子组")
}
} else {
fmt.Println("您被淘汰了!请下次努力!")
}
}
//输出结果如下
恭喜进入决赛!
您进入的是男子组
4、switch 分支
switch 语句用于基于不同条件执行不同动作,每一个 case 分支都是唯一的,从上至下逐一测试,直到匹配为止
switch 语句执行的过程从上至下,直到找到匹配项,匹配项后面也不需要再加 break
switch 默认情况下 case 最后自带break 语句,匹配成功后就不会执行其他 case ,如果我们需要执行后面的 case ,可以使用 fallthrouth
switch 语句的语法如下:
switch var1 {
case val1:
...
case val2:
...
default:
...
}
变量 var1 可以是任何类型,而 val1 和val2 则可以是同类型的任意值,类型不被局限于常量或整数,但是必须是相同的类型;或者最终结果为相同类型的表达式
//Switch语句 多条件
//对学生成绩进行等级考评,90-100:优秀;80-89:良好;60-79 合格;60以下 不及格。
package main
import "fmt"
func main() {
var source = 77
//等效于if...else if 多分支语句
switch {
case source >= 90 && source <= 100:
fmt.Println("优秀")
case source >= 80 && source <= 89:
fmt.Println("良好")
case source >= 60 && source <= 79:
fmt.Println("合格")
default:
fmt.Println("不合格")
}
}
package main
import "fmt"
func main() {
var vaule = 2
switch vaule {
case 1:
fmt.Println("菜单1功能")
case 2:
fmt.Println("菜单2功能")
case 3:
fmt.Println("菜单3功能")
default:
fmt.Println("输入错误,请重新输入!")
}
}
//输入结果如下
菜单2功能
5、Type Switch
switch 语句还可以被用于 type-switch 来判断某个 interface 变量中实际存储的变量类型
Type Switch 语法格式如下:
switch x.(type){
case type:
statement(s);
case type:
statement(s);
//你可以定义任意个数的case
default: /* 可选 */
statement(s);
}
package main
import "fmt"
func main() {
//定义空接口
var x interface{}
//判断数据类型
switch i := x.(type) {
case nil:
fmt.Printf("数据类型是%T", i)
case int:
fmt.Println("数据类型是int")
case float32, float64:
fmt.Println("数据类型是浮点型")
case string:
fmt.Println("数据类型是字符串")
case bool:
fmt.Println("数据类型是布尔型")
default:
fmt.Println("数据类型是其他类型")
}
}
//输出结果是:
数据类型是<nil>
package main
import "fmt"
// 全局变量可以不被调用,函数中声明的变量一定要被调用
func main() {
var num = 10
make_type(number)
}
//定义判断数据类型
func make_type(x interface{}) {
// 判断数据类型 type 只能使用搭配 Switch,如果想单独使用输出数据类型,需要使用反射
switch i := x.(type) {
case nil:
fmt.Printf("数据类型为 %T",i)
case int:
fmt.Println("数据类型为 int")
case float32,float64:
fmt.Println("数据类型为浮点型")
case string:
fmt.Println("数据类型为字符串")
case bool:
fmt.Println("数据类型为 bool")
default:
fmt.Println("其他类型")
}
}
//输出结果为:
数据类型为整数型
//反射判断数据类型,这种方式比较灵活
package main
import (
"fmt"
"reflect"
)
func main() {
var num *int
fmt.Println("num 的数据类型:",reflect.TypeOf(num))
}
//输出结果为:
num 的数据类型: *int
6、fallthrough
fallthrough 会强制执行后面的 case 语句,fallthrough 不会判断下一条 case 的表达式结果是否为 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")
}
}
//输出结果为:
2、case 条件语句为 true
3、case 条件语句为 false
4、case 条件语句为 true
7、select 语句
select 是 Go 中的一个控制结构,类似于用于通信的 switch 语句。每个 case 必须是一个通信操作,要么是发送要么是接收
select 随机执行一个可运行的 case。如果没有 case 可运行,它将阻塞,直到有 case 可运行。一个默认的子句应该总是可运行的
select {
case communication clause :
statement(s);
case communication clause :
statement(s);
/* 你可以定义任意数量的 case */
default : /* 可选 */
statement(s);
}
以下描述了 selcet 语句的语法:
每个 case 都必须是一个通信。
所有 channel 表达式都会被求值。
所有被发送的表达式都会被求值。
如果任意某个通信可以进行,它就执行,其他被忽略。如果有多个 case 都可以运行,Select 会随机公平地选出一个执行。其他不会执行。
否则:
如果有 default 子句,则执行该语句。
如果没有 default 子句,select 将阻塞,直到某个通信可以运行;Go 不会重新对 channel 或值进行求值。
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")
}
}
//输出结果:
no communication
package main
import (
"fmt"
"time"
)
func Chann(ch chan int, stopCh chan bool) {
for j := 0; j < 10; j++ {
ch <- j
time.Sleep(time.Second)
}
stopCh <- true
}
func main() {
ch := make(chan int)
c := 0
stopCh := make(chan bool)
go Chann(ch, stopCh)
for {
select {
case c = <-ch:
fmt.Println("Receive C", c)
case s := <-ch:
fmt.Println("Receive S", s)
case _ = <-stopCh:
goto end
}
}
end:
}
//输出结果为:
Receive S 0
Receive C 1
Receive S 2
Receive S 3
Receive C 4
Receive S 5
Receive S 6
Receive S 7
Receive C 8
Receive S 9