Go语言教程之边写边学:Switch…Case条件语句

在本教程中,您将学习如何使用switch-case语句根据Golang中的不同条件执行不同的操作。

Golang 还支持类似于其他语言(如Php或Java)中的 switch 语句。switch 语句是一种替代方法,用于根据变量的状态将冗长的 if else 比较表达为更具可读性的代码。

 

switch语句

switch语句用于选择要执行的多个代码块之一。

请考虑以下示例,该示例显示特定日期的不同消息。

package main
 
import (
	"fmt"
	"time"
)
 
func main() {
	today := time.Now()
 
	switch today.Day() {
	case 5:
		fmt.Println("Today is 5th. Clean your house.")
	case 10:
		fmt.Println("Today is 10th. Buy some wine.")
	case 15:
		fmt.Println("Today is 15th. Visit a doctor.")
	case 25:
		fmt.Println("Today is 25th. Buy some food.")
	case 31:
		fmt.Println("Party tonight.")
	default:
		fmt.Println("No information available for that day.")
	}
}

如果未找到匹配项,则使用default语句。

 

case拥有多个条件

case带有多个条件,用于为许多类似情况选择公共代码块。

package main
 
import (
	"fmt"
	"time"
)
 
func main() {
	today := time.Now()
	var t int = today.Day()
 
	switch t {
	case 5, 10, 15:
		fmt.Println("Clean your house.")
	case 25, 26, 27:
		fmt.Println("Buy some food.")
	case 31:
		fmt.Println("Party tonight.")
	default:
		fmt.Println("No information available for that day.")
	}
}

 

fallthrough关键字

fallthrough关键字,用于强制执行流通过连续的事例块。

package main
 
import (
	"fmt"
	"time"
)
 
func main() {
	today := time.Now()
 
	switch today.Day() {
	case 5:
		fmt.Println("Clean your house.")
		fallthrough
	case 10:
		fmt.Println("Buy some wine.")
		fallthrough
	case 15:
		fmt.Println("Visit a doctor.")
		fallthrough
	case 25:
		fmt.Println("Buy some food.")
		fallthrough
	case 31:
		fmt.Println("Party tonight.")
	default:
		fmt.Println("No information available for that day.")
	}
}

下面是每月 10 日的输出。

Buy some wine.
Visit a doctor.
Buy some food.
Party tonight.

 

有条件case

case语句还可以与条件运算符一起使用。

package main
 
import (
	"fmt"
	"time"
)
 
func main() {
	today := time.Now()
 
	switch {
	case today.Day() < 5:
		fmt.Println("Clean your house.")
	case today.Day() <= 10:
		fmt.Println("Buy some wine.")
	case today.Day() > 15:
		fmt.Println("Visit a doctor.")
	case today.Day() == 25:
		fmt.Println("Buy some food.")
	default:
		fmt.Println("No information available for that day.")
	}
}

 

switch初始值设定项语句

switch关键字可以紧跟一个简单的初始化语句,其中可以声明和初始化switch代码块的本地变量。

package main
 
import (
	"fmt"
	"time"
)
 
func main() {
	switch today := time.Now(); {
	case today.Day() < 5:
		fmt.Println("Clean your house.")
	case today.Day() <= 10:
		fmt.Println("Buy some wine.")
	case today.Day() > 15:
		fmt.Println("Visit a doctor.")
	case today.Day() == 25:
		fmt.Println("Buy some food.")
	default:
		fmt.Println("No information available for that day.")
	}
}
Go语言教程之边写边学:If...Else...Else If条件语句

if else

在本教程中,您将学习如何编写用于在 Golang 中执行不同操作的决策条件语句。

像大多数编程语言一样,Golang 从 C 系列语言中借用了几种控制流语法。在 Golang 中,我们有以下条件语句:

  • if语句 - 如果一个条件为真,则执行一些代码
  • if...else语句 - 如果条件为真,则执行一些代码,如果条件为假,则执行另一个代码
  • if…else if....else语句 - 对两个以上的条件执行不同的代码
  • switch...case语句 - 选择要执行的众多代码块之一

我们将在接下来的部分中探讨这些陈述中的每一个。

 

if 语句

if语句用于仅在指定条件的计算结果为 true 时才执行代码块。

if  condition { 
    // code to be executed if condition is true
}

下面的示例将输出“日本”,如果X为真:

package main
 
import (
	"fmt"
)
 
func main() {
	var s = "Japan"
	x := true
	if x {
		fmt.Println(s)
	}
}

 

If…else语句

语句允许您在指定的条件计算结果为 true 时执行一个代码块,如果计算结果为 false,则执行另一个代码块。

if  condition { 
    // code to be executed if condition is true
} else {
    // code to be executed if condition is false
}

下面的示例将输出“日本”,如果X为100:

package main
 
import (
	"fmt"
)
 
func main() {
	x := 100
 
	if x == 100 {
		fmt.Println("Japan")
	} else {
		fmt.Println("Canada")
	}
}

 

if...else if...else语句

if … else if … else语句允许组合多个else if。

if  condition-1 { 
    // code to be executed if condition-1 is true
} else if condition-2 {
    // code to be executed if condition-2 is true
} else {
    // code to be executed if both condition1 and condition2 are false
}

下面的示例将输出“日本”,如果X为100:

package main
 
import (
	"fmt"
)
 
func main() {
	x := 100
 
	if x == 50 {
		fmt.Println("Germany")
	} else if x == 100 {
		fmt.Println("Japan")
	} else {
		fmt.Println("Canada")
	}
}

 

if语句初始化

If语句支持复合语法,其中测试的表达式前面有一个初始化语句。

if  var declaration;  condition { 
    // code to be executed if condition is true
}

如果X为100,下面的示例将输出“德国”:

package main
 
import (
	"fmt"
)
 
func main() {
	if x := 100; x == 100 {
		fmt.Println("Germany")
	}
}
  • 当前日期:
  • 北京时间:
  • 时间戳:
  • 今年的第:18周
  • 我的 IP:3.129.218.83
农历
五行
冲煞
彭祖
方位
吉神
凶神
极简任务管理 help
+ 0 0 0
Task Idea Collect