Golang 实现字符串的switch语句
在Go语言中,switch语句是一种控制流语句,它可以根据不同的情况运行一个代码块。类似于if-else表达式,它可以更快地测试多个条件。在本文中,我们将看到如何在Go语言中对字符串使用switch语句。
方法1:将默认情况作为回退选项
在这种方法中,如果检测到匹配项,则程序将验证输入变量,将其与不同情况进行比较,然后执行相应的代码块;否则,它将运行默认情况的代码块。让我们通过示例和算法看看它是如何执行的。
步骤
- 步骤1 - 在程序中创建一个名为main的包,并声明fmt(格式化包)包。其中,main用于生成可执行示例,fmt用于格式化输入和输出。
-
步骤2 - 创建一个名为main的函数,在该函数中创建一个名为input value的变量,并将其初始化为字符串”option2″。
-
步骤3 - 在switch关键字后使用变量input_value。
-
步骤4 - 使用case关键字后面跟随一个字符串值来指定switch语句的可能情况。
-
步骤5 - 如果变量input_value与给定的case选项匹配,则执行应该运行的示例。
-
步骤6 - 使用default case作为回退选项来处理任何不匹配任何case选项的输入。
-
步骤7 - 使用fmt.Println()函数来打印所选的选择,其中ln表示换行。
示例
在本程序中,我们将学习如何在字符串上执行switch语句,并将默认情况作为回退选项。
package main
import "fmt"
func main() {
fmt.Println("The switch statement is executed like:")
input_value := "option2" //create an input value
switch input_value {
case "option1":
fmt.Println("Option 1 selected")
case "option2":
fmt.Println("Option 2 selected")
case "option3":
fmt.Println("Option 3 selected")
default:
fmt.Println("Invalid option selected") //set a default case
}
}
输出
The switch statement is executed like:
Option 2 selected
方法2:使用fallthrough关键字
在这个方法中,fallthrough关键字用于将执行转发到下一个case块。在这个示例中,如果输入是”option2″,程序将运行”option2″被选中和”option3″被选中的Example:块。
步骤
- 步骤1 - 创建一个主要的包并在程序中声明fmt(格式化包)包,其中main产生可执行示例和fmt在输入和输出的格式化中起作用。
-
步骤2 - 根据上述程序重复执行步骤4,并在选项1、2和3中添加fallthrough。
-
步骤3 - 使用默认案例作为选择了无效选项。
-
步骤4 - 借助fallthrough,下一个块的执行将继续。
-
步骤5 - 使用fmt.Println()函数将输出打印到控制台,其中ln表示新行。
示例
在这个示例中,我们将看到如何使用fallthrough关键字在字符串上执行switch语句。
package main
import "fmt"
func main() {
input_value := "option2" //create an input value
fmt.Println("The switch statement is executed like:")
switch input_value {
case "option1":
fmt.Println("Option 1 selected")
fallthrough
case "option2":
fmt.Println("Option 2 selected")
fallthrough
case "option3":
fmt.Println("Option 3 selected")
default:
fmt.Println("Invalid option selected") //set default case
}
}
输出
The switch statement is executed like:
Option 2 selected
Option 3 selected
方法3:使用Maps
在这种方法中,如果找到匹配项,程序将运行关联的函数;否则,将运行默认情况。该程序将验证输入变量并将其与地图中的键进行比较。
步骤
- 步骤1 - 在程序中创建一个主要(main)包并声明fmt(format包)包,其中main生成可执行示例并fmt帮助格式化输入和输出。
-
步骤2 - 创建一个名为input_value的变量并将其初始化为字符串”option2″。
-
步骤3 - 创建一个名为options的地图,其中值是匹配的Example:块,键是字符串选择。
-
步骤4 - 使用if语句来确定输入变量是否用作选项地图中的键。
-
步骤5 - 如果键存在,则调用保存为键值的函数来运行匹配的Example:块。
-
步骤6 - 如果键缺失,则运行一个默认情况块,该块打印”Invalid choice selected”。
示例
在此程序中,我们将学习如何使用地图在字符串上实现switch语句。
package main
import "fmt"
func main() {
input_value := "option2" //create input value
fmt.Println("The switch statement is executed like:")
options := map[string]func(){ //create a map
"option1": func() { fmt.Println("Option 1 selected") },
"option2": func() { fmt.Println("Option 2 selected") },
"option3": func() { fmt.Println("Option 3 selected") },
}
if fn, ok := options[input_value]; ok {
fn()
} else {
fmt.Println("Invalid option selected")
}
}
输出
The switch statement is executed like:
Option 2 selected
结论
我们执行了在字符串上实现switch语句的程序,并且给出了三个示例。在第一个示例中,我们使用了简单的switch case语句,在第二个示例中,我们使用了fallthrough关键字,在第三个示例中,我们使用了map。