Go 语言条件语句

1、Go 语言条件语句

条件语句需要开发者通过指定一个或多个条件,并通过测试条件是否为 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可运行。

注意:Go 没有三目运算符,所以不支持 ?: 形式的条件判断。

1.1 Go 语言 if 语句

if 语句由布尔表达式后紧跟一个或多个语句组成。

Go 编程语言中 if 语句的语法如下:

if 布尔表达式 {
   /* 在布尔表达式为 true 时执行 */
}

If 在布尔表达式为 true 时,其后紧跟的语句块执行,如果为 false 则不执行。

// 使用 if 判断一个数变量的大小
package main

import "fmt"

func main() {
	/* 定义局部变量 */
	var a int = 10
	/* 使用 if 语句判断布尔表达式 */
	if a < 20 {
		/* 如果条件为 true 则执行以下语句 */
		// a 小于 20
		fmt.Printf("a 小于 20\n")
	}
	// a 的值为 : 10
	fmt.Printf("a 的值为 : %d\n", a)
}

if 语句使用 tips:

(1)不需使用括号将条件包含起来

(2)大括号{}必须存在,即使只有一行语句

(3)左括号必须在if或else的同一行

(4)在if之后,条件语句之前,可以添加变量初始化语句,使用;进行分隔

(5)在有返回值的函数中,最终的return不能在条件语句中

if 还有另外一种形式,它包含一个 statement 可选语句部分,该组件在条件判断之前运行。它的语法是:

if statement; condition {  
}

实例:

package main

import (
	"fmt"
)

func main() {
	// 判断数字是否为偶数
	// 10 偶数
	if num := 10; num%2 == 0 {
		fmt.Println(num, "偶数")
	} else {
		fmt.Println(num, "奇数")
	}
}

1.2 Go 语言 if…else 语句

if 语句后可以使用可选的 else 语句, else 语句中的表达式在布尔表达式为 false 时执行。

Go 编程语言中 if…else 语句的语法如下:

if 布尔表达式 {
   /* 在布尔表达式为 true 时执行 */
} else {
  /* 在布尔表达式为 false 时执行 */
}

If 在布尔表达式为 true 时,其后紧跟的语句块执行,如果为 false 则执行 else 语句块。

package main

import "fmt"

func main() {
	/* 局部变量定义 */
	var a int = 100
	/* 判断布尔表达式 */
	if a < 20 {
		/* 如果条件为 true 则执行以下语句 */
		fmt.Printf("a 小于 20\n")
	} else {
		/* 如果条件为 false 则执行以下语句 */
		// a 不小于 20
		fmt.Printf("a 不小于 20\n")
	}
	// a 的值为 : 100
	fmt.Printf("a 的值为 : %d\n", a)
}

if ... else if ... else...实例:

package main

import "fmt"

func main() {
	// too small
	var age int = 23
	if age == 25 {
		fmt.Println("true")
	} else if age < 25 {
		fmt.Println("too small")
	} else {
		fmt.Println("too big")
	}
}

1.3 Go 语言 if 语句嵌套

你可以在 if 或 else if 语句中嵌入一个或多个 if 或 else if 语句。

Go 编程语言中 if…else 语句的语法如下:

if 布尔表达式 1 {
   /* 在布尔表达式 1 为 true 时执行 */
   if 布尔表达式 2 {
      /* 在布尔表达式 2 为 true 时执行 */
   }
}

你可以以同样的方式在 if 语句中嵌套 else if...else 语句

// 嵌套使用 if 语句
package main

import "fmt"

func main() {
	/* 定义局部变量 */
	var a int = 100
	var b int = 200
	/* 判断条件 */
	if a == 100 {
		/* if 条件语句为 true 执行 */
		if b == 200 {
			// a 的值为 100 , b 的值为 200
			/* if 条件语句为 true 执行 */
			fmt.Printf("a 的值为 100 , b 的值为 200\n")
		}
	}
	// a 值为 : 100
	fmt.Printf("a 值为 : %d\n", a)
	// b 值为 : 200
	fmt.Printf("b 值为 : %d\n", b)
}

1.4 Go 语言 switch 语句

switch 语句用于基于不同条件执行不同动作,每一个 case 分支都是唯一的,从上至下逐一测试,直到匹配为止。

switch 语句执行的过程从上至下,直到找到匹配项,匹配项后面也不需要再加 break。

switch 默认情况下 case 最后自带 break 语句,匹配成功后就不会执行其他 case,如果我们需要执行后面的

case,可以使用 fallthrough 。

Go 编程语言中 switch 语句的语法如下:

switch var1 {
    case val1:
        ...
    case val2:
        ...
    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 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")
	}
	// 你的等级是 A
	fmt.Printf("你的等级是 %s\n", grade)
}

1.4.1 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{}
	// x 的类型 :
	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("未知型")
	}
}

1.4.2 fallthrough

使用 fallthrough 会强制执行后面的 case 语句,fallthrough 不会判断下一条 case 的表达式结果是否为 true。

package main

import "fmt"

func main() {
	switch {
	case false:
		fmt.Println("1、case 条件语句为 false")
		fallthrough
	case true:
		// 2、case 条件语句为 true
		fmt.Println("2、case 条件语句为 true")
		fallthrough
	case false:
		// 3、case 条件语句为 false
		fmt.Println("3、case 条件语句为 false")
		fallthrough
	case true:
		// 4、case 条件语句为 true
		fmt.Println("4、case 条件语句为 true")
	case false:
		fmt.Println("5、case 条件语句为 false")
		fallthrough
	default:
		fmt.Println("6、默认 case")
	}
}

从以上代码输出的结果可以看出:switch 从第一个判断表达式为 true 的 case 开始执行,如果 case 带有

fallthrough,程序会继续执行下一条 case,且它不会去判断下一个 case 的表达式是否为 true。

1.4.3 switch使用注意事项

1、支持多条件匹配

switch{
    case 1,2,3,4:
    default:
}

2、不同的 case 之间不使用 break 分隔,默认只会执行一个 case。

3、如果想要执行多个 case,需要使用 fallthrough 关键字,也可用 break 终止。

switch{
    case 1:
    ...
    if(...){
        break
    }

    fallthrough // 此时switch(1)会执行case1和case2,但是如果满足if条件,则只执行case1

    case 2:
    ...
    case 3:
}

4、switch 的 default 不论放在哪都是最后执行。

a := 10

switch {
   default : {
      fmt.Println("default")
   }
   case a > 0 : {
      fmt.Println("a > 0")
   }
   case a >5 : {
      fmt.Println("a > 5")
   }
}
a := 10
switch {
   case a > 0 : {
      fmt.Println("a > 0")
   }
   case a >5 : {
      fmt.Println("a > 5")
   }
               default : {
      fmt.Println("default")
   }
}

上面两段代码没有区别。

1.5 Go 语言 select 语句

select 是 Go 中的一个控制结构,类似于 switch 语句。

select 语句只能用于通道操作,每个 case 必须是一个通道操作,要么是发送要么是接收。

select 语句会监听所有指定的通道上的操作,一旦其中一个通道准备好就会执行相应的代码块。

如果多个通道都准备好,那么 select 语句会随机选择一个通道执行。如果所有通道都没有准备好,那么执行

default 块中的代码。

Go 编程语言中 select 语句的语法如下:

select {
  case <- channel1:
    // 执行的代码
  case value := <- channel2:
    // 执行的代码
  case channel3 <- value:
    // 执行的代码

    // 你可以定义任意数量的 case

  default:
    // 所有通道都没有准备好,执行的代码
}

以下描述了 select 语句的语法:

  • 每个 case 都必须是一个通信

  • 所有 channel 表达式都会被求值

  • 所有被发送的表达式都会被求值

  • 如果任意某个通信可以进行,它就执行,其他被忽略。

  • 如果有多个 case 都可以运行,Select 会随机公平地选出一个执行。其他不会执行。

    否则:如果有 default 子句,则执行该语句。如果没有 default 子句,select 将阻塞,直到某个通信可以运

    行;Go 不会重新对 channel 或值进行求值。

实例1:

package main

import "fmt"

func main() {
	var c1, c2, c3 chan int
	var i1, i2 int
	// 输出结果:no communication
	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")
	}
}

实例2:

package main

import (
	"fmt"
	"time"
)

func main() {

	c1 := make(chan string)
	c2 := make(chan string)

	go func() {
		time.Sleep(1 * time.Second)
		c1 <- "one"
	}()

	go func() {
		time.Sleep(2 * time.Second)
		c2 <- "two"
	}()

	/*
	received one
	received two
	 */
	for i := 0; i < 2; i++ {
		select {
		case msg1 := <-c1:
			fmt.Println("received", msg1)
		case msg2 := <-c2:
			fmt.Println("received", msg2)
		}
	}
}

实例3:

package main

import "fmt"

func main() {
	// 定义两个通道
	ch1 := make(chan string)
	ch2 := make(chan string)

	// 启动两个 goroutine,分别从两个通道中获取数据
	go func() {
		for {
			ch1 <- "from 1"
		}
	}()
	go func() {
		for {
			ch2 <- "from 2"
		}
	}()

	/*
	from 1
	from 2
	no message received
	上面3条不断循环输出
	 */
	// 使用 select 语句非阻塞地从两个通道中获取数据
	for {
		select {
		case msg1 := <-ch1:
			fmt.Println(msg1)
		case msg2 := <-ch2:
			fmt.Println(msg2)
		default:
			// 如果两个通道都没有可用的数据,则执行这里的语句
			fmt.Println("no message received")
		}
	}
}

select 是随机执行的不是循环检测,是为了避免饥饿问题:

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)
	/*
		    Receive S 0
			Receive S 1
			Receive S 2
			Receive C 3
			Receive C 4
			Receive S 5
			Receive C 6
			Receive S 7
			Receive S 8
			Receive C 9

	*/
	for {
		select {
		case c = <-ch:
			fmt.Println("Receive C", c)
		case s := <-ch:
			fmt.Println("Receive S", s)
		case _ = <-stopCh:
			goto end
		}
	}
end:
}

你可能感兴趣的:(golang,golang)