GoLang 学习笔记 – 条件语句
条件语句通过指定一个或多个条件,并检测条件是否为 true
来决定是否执行指定语句,并在条件为 false
的情况下执行另外的语句。
GoLang 提供了以下几种条件判断语句:
语句 | 描述 |
---|---|
if | if 语句由一个布尔表达式后紧跟一个或多个语句组成。 |
if…else | if 语句后可以使用可选的 else 语句, else 语句中的表达式在布尔表达式为 false 时执行。 |
if 嵌套 | 你可以在 if 或 else if 语句中嵌入一个或多个 if 或 else if 语句。 |
switch | switch 语句用于基于不同条件执行不同动作。 |
select | select 语句类似于 switch 语句,但是 select 会随机执行一个可运行的 case。如果没有 case 可运行,它将阻塞,直到有case可运行。 |
注意:GoLang 没有三目运算符,所以不支持 ? :
形式的条件判断。
if 语句
GoLang 中 if 语句的语法如下:
if [布尔表达式] { /* 在布尔表达式为 true 时执行 */ }
注意条件不需要用小括号 ( )
包裹
,如:
var a int = 10 if a < 20 { fmt.Println("abc") } fmt.Println("def") // 输出结果为 // abc // def
if … else 语句
GoLang 中 if … else 语句的语法如下:
if [布尔表达式] { /* 在布尔表达式为 true 时执行 */ } else { /* 在布尔表达式为 false 时执行 */ }
if 嵌套语句
GoLang 中 if 嵌套语句的语法如下:
if [布尔表达式] { /* 在 布尔表达式 1 为 true 时执行 */ if [布尔表达式 2] { /* 在 布尔表达式 2 为 true 时执行 */ } }
switch 语句
switch 语句用于基于不同条件执行不同动作,每一个 case 分支都是唯一的,从上至下逐一测试,直到匹配为止。
switch 语句执行的过程从上至下,直到找到匹配项,匹配项后面不需要再加 break
。
GoLang 中 switch 默认情况下 case 最后自带 break
语句,匹配成功后就不会执行其他 case,如果我们需要执行后面的 case,可以使用 fallthrough
。
GoLang 中 if 嵌套语句的语法如下:
switch var1 { case val1: // 当val1 等于 var1时执行 case val2: // 当val1 = var2时执行 default: // 没有匹配的值时执行 }
变量 var1 可以是任何类型,而 val1 和 val2 则可以是同类型的任意值。类型不被局限于常量或整数,但必须是相同的类型,或者最终结果为相同类型的表达式。
您可以同时测试多个可能符合条件的值,使用逗号分割它们,例如:case val1, val2, val3。
package main import "fmt" func main() { /* 定义局部变量 */ var grade string = "B" var marks int = 90 switch marks { case 90: grade = "A" case 80: grade = "B" case 50,60,70 : grade = "C" // 此处测试多个值 default: grade = "D" } switch { // 如果这里不写 需要测试的变量的话,需要在每个 case 后面写完整的条件。 case grade == "A" : fmt.Printf("优秀!\n" ) case grade == "B", grade == "C" : fmt.Printf("良好\n" ) case grade == "D" : fmt.Printf("及格\n" ) case grade == "F": fmt.Printf("不及格\n" ) default: fmt.Printf("差\n" ); } fmt.Printf("你的等级是 %s\n", grade ); } // 执行结果为 // 优秀! // 你的等级是 A
Type Switch
switch 语句还可以被用于 type-switch 来判断某个 interface 变量中实际存储的变量类型,语法格式如下:
switch x.(type){ case type: statement(s); case type: statement(s); default: /* 可选 */ statement(s); }
如:
package main import "fmt" func main() { var x interface{} switch i := x.(type) { case nil: fmt.Printf(" x 的类型 :%T",i) case int: fmt.Printf("x 是 int 型") case float64: fmt.Printf("x 是 float64 型") case func(int) float64: fmt.Printf("x 是 func(int) 型") case bool, string: fmt.Printf("x 是 bool 或 string 型" ) default: fmt.Printf("未知型") } } // 执行结果为 // x 的类型 :
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
从以上代码输出的结果可以看出:switch 从第一个判断表达式为 true 的 case 开始执行,如果 case 带有 fallthrough,程序会继续执行下一条 case,且它不会去判断下一个 case 的表达式是否为 true。
select 语句
select 是 GoLang 中的一个控制结构,类似用于通信的 switch 语句。每个 case 必须是一个通信操作,要么是发送要么是接收。
select 随机执行一个可运行的 case。如果没有 case 可运行,它将阻塞,直到有 case 可运行。一个默认的子句应该总是可运行的。
GoLang 中 select 语句的语法如下:
select { case communication clause : statement(s); case communication clause : statement(s); /* 你可以定义任意数量的 case */ default : /* 可选 */ statement(s); }
以下描述了 select 语句的语法:
- 每个 case 都必须是一个通信
- 所有 channel 表达式都会被求值
- 所有被发送的表达式都会被求值
- 如果任意某个通信可以进行,它就执行,其他被忽略。
- 如果有多个 case 都可以运行,select 会随机公平地选出一个执行。其他不会执行。
-
否则:
- 如果有 default 子句,则执行该语句。
- 如果没有 default 子句,select 将阻塞(select 会循环检测条件,如果有满足的条件则执行并退出,否则一直循环检测。),直到某个通信可以运行;GoLang不会重新对 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
以上内容均源于网络,并加上自己的实践和理解,如有错误的地方,请在评论区指正。
//