您的位置:首页 > 文旅 > 美景 > 网页特效经典300例_个人免费网站怎么建设_推广网址_百度搜索引擎关键词

网页特效经典300例_个人免费网站怎么建设_推广网址_百度搜索引擎关键词

2024/12/23 7:26:10 来源:https://blog.csdn.net/LuckyLay/article/details/144518994  浏览:    关键词:网页特效经典300例_个人免费网站怎么建设_推广网址_百度搜索引擎关键词
网页特效经典300例_个人免费网站怎么建设_推广网址_百度搜索引擎关键词

Golang学习笔记_07——基本类型
Golang学习笔记_08——For循环
Golang学习笔记_09——if条件判断


文章目录

    • Switch语句
      • 1. 基本用法
      • 2. 多个表达式匹配同一个case
      • 3. 不带表达式的switch(相当于`if-else if-else`)
        • 4. 使用`fallthrough`关键字
      • 5. Type Switch(类型开关)
      • 6. 注意事项
    • 源码


Switch语句

1. 基本用法

在Go语言中,switch语句是一种多分支选择结构,用于替代多个if-else语句,使代码更加简洁和易读。switch语句可以基于一个或多个表达式进行匹配,并执行相应的代码块。

switch expr {
case value1:// 当 expr == value1 时执行的代码
case value2:// 当 expr == value2 时执行的代码
default:// 当 expr 不匹配任何 case 时执行的代码
}
  • expr:要评估的表达式。
  • case valueN:与expr进行比较的值。
  • default:可选的默认分支,当expr不匹配任何case时执行。

举个例子

func switchDemo(inputNum int) {num := inputNumswitch num {case 1:fmt.Println("One")case 2:fmt.Println("Two")case 3:fmt.Println("Three")default:fmt.Println("Other")}
}

测试方法

func Test_switchDemo(t *testing.T) {type args struct {inputNum int}tests := []struct {name stringargs args}{{name: "1",args: args{inputNum: 1},},{name: "other",args: args{inputNum: 100},},}for _, tt := range tests {t.Run(tt.name, func(t *testing.T) {switchDemo(tt.args.inputNum)})}
}

输出结果

=== RUN   Test_switchDemo
=== RUN   Test_switchDemo/1
One
=== RUN   Test_switchDemo/other
Other
--- PASS: Test_switchDemo (0.00s)--- PASS: Test_switchDemo/1 (0.00s)--- PASS: Test_switchDemo/other (0.00s)
PASS

2. 多个表达式匹配同一个case

举个例子

func mutSwitchDemo(aChar byte) {switch aChar {case 'a', 'e', 'i', 'o', 'u':fmt.Println("Vowel")default:fmt.Println("Consonant")}
}

测试方法

func Test_mutSwitchDemo(t *testing.T) {type args struct {aChar byte}tests := []struct {name stringargs args}{{name: "a",args: args{aChar: 'a'},},{name: "z",args: args{aChar: 'z'},},}for _, tt := range tests {t.Run(tt.name, func(t *testing.T) {mutSwitchDemo(tt.args.aChar)})}
}

输出结果

=== RUN   Test_mutSwitchDemo
=== RUN   Test_mutSwitchDemo/a
Vowel
=== RUN   Test_mutSwitchDemo/z
Consonant
--- PASS: Test_mutSwitchDemo (0.00s)--- PASS: Test_mutSwitchDemo/a (0.00s)--- PASS: Test_mutSwitchDemo/z (0.00s)
PASS

3. 不带表达式的switch(相当于if-else if-else

举个例子

func noSwitchDemo(inputNum int) {num := inputNumswitch {case num < 0:fmt.Println("Negative")case num == 0:fmt.Println("Zero")case num > 0:fmt.Println("Positive")}
}

测试方法

func Test_noSwitchDemo(t *testing.T) {type args struct {inputNum int}tests := []struct {name stringargs args}{{name: "Negative",args: args{inputNum: -100},},{name: "zero",args: args{inputNum: 0},},{name: "Positive",args: args{inputNum: 100},},}for _, tt := range tests {t.Run(tt.name, func(t *testing.T) {noSwitchDemo(tt.args.inputNum)})}
}

输出结果

=== RUN   Test_noSwitchDemo
=== RUN   Test_noSwitchDemo/Negative
Negative
=== RUN   Test_noSwitchDemo/zero
Zero
=== RUN   Test_noSwitchDemo/Positive
Positive
--- PASS: Test_noSwitchDemo (0.00s)--- PASS: Test_noSwitchDemo/Negative (0.00s)--- PASS: Test_noSwitchDemo/zero (0.00s)--- PASS: Test_noSwitchDemo/Positive (0.00s)
PASS
4. 使用fallthrough关键字

fallthrough关键字会使程序继续执行下一个case语句,即使当前case已经匹配成功。

func fullThroughDemo(inputNum int) {num := inputNumswitch num {case 1:fmt.Println("One")fallthroughcase 2:fmt.Println("Two (fallthrough)")case 3:fmt.Println("Three")default:fmt.Println("Other")}
}

测试方法

func Test_fullThroughDemo(t *testing.T) {type args struct {inputNum int}tests := []struct {name stringargs args}{{name: "1",args: args{inputNum: 1},},}for _, tt := range tests {t.Run(tt.name, func(t *testing.T) {fullThroughDemo(tt.args.inputNum)})}
}

输出结果

=== RUN   Test_fullThroughDemo
=== RUN   Test_fullThroughDemo/1
One
Two (fallthrough)
--- PASS: Test_fullThroughDemo (0.00s)--- PASS: Test_fullThroughDemo/1 (0.00s)
PASS

5. Type Switch(类型开关)

举个例子

func typeSwitchDemo(i interface{}) {switch v := i.(type) {case int:fmt.Printf("Type is int: %d\n", v)case string:fmt.Printf("Type is string: %s\n", v)case bool:fmt.Printf("Type is bool: %t\n", v)default:fmt.Println("Unknown type")}
}

测试方法

func Test_typeSwitchDemo(t *testing.T) {type args struct {i interface{}}tests := []struct {name stringargs args}{{name: "bool",args: args{i: true},},{name: "int",args: args{i: 1},},{name: "string",args: args{i: "hello"},},}for _, tt := range tests {t.Run(tt.name, func(t *testing.T) {typeSwitchDemo(tt.args.i)})}
}

输出结果

=== RUN   Test_typeSwitchDemo
=== RUN   Test_typeSwitchDemo/bool
Type is bool: true
=== RUN   Test_typeSwitchDemo/int
Type is int: 1
=== RUN   Test_typeSwitchDemo/string
Type is string: hello
--- PASS: Test_typeSwitchDemo (0.00s)--- PASS: Test_typeSwitchDemo/bool (0.00s)--- PASS: Test_typeSwitchDemo/int (0.00s)--- PASS: Test_typeSwitchDemo/string (0.00s)
PASS

6. 注意事项

  1. switchcase 语句从上到下顺次执行,直到匹配成功时停止。
  2. 每个case块以break隐式结束:在Go语言中,每个case块在执行完毕后会自动跳出switch语句,因此不需要显式地使用break
  3. fallthrough的使用fallthrough关键字是可选的,并且通常不常用,因为它会使代码的逻辑变得复杂。
  4. 类型开关:类型开关是Go语言特有的功能,非常适用于处理接口类型的变量。

源码

// switch_demo.go 文件
package switch_demoimport "fmt"func switchDemo(inputNum int) {num := inputNumswitch num {case 1:fmt.Println("One")case 2:fmt.Println("Two")case 3:fmt.Println("Three")default:fmt.Println("Other")}
}func mutSwitchDemo(aChar byte) {switch aChar {case 'a', 'e', 'i', 'o', 'u':fmt.Println("Vowel")default:fmt.Println("Consonant")}
}func noSwitchDemo(inputNum int) {num := inputNumswitch {case num < 0:fmt.Println("Negative")case num == 0:fmt.Println("Zero")case num > 0:fmt.Println("Positive")}
}func fullThroughDemo(inputNum int) {num := inputNumswitch num {case 1:fmt.Println("One")fallthroughcase 2:fmt.Println("Two (fallthrough)")case 3:fmt.Println("Three")default:fmt.Println("Other")}
}func typeSwitchDemo(i interface{}) {switch v := i.(type) {case int:fmt.Printf("Type is int: %d\n", v)case string:fmt.Printf("Type is string: %s\n", v)case bool:fmt.Printf("Type is bool: %t\n", v)default:fmt.Println("Unknown type")}
}
// switch_demo_test.go
package switch_demoimport ("testing"
)func Test_switchDemo(t *testing.T) {type args struct {inputNum int}tests := []struct {name stringargs args}{{name: "1",args: args{inputNum: 1},},{name: "other",args: args{inputNum: 100},},}for _, tt := range tests {t.Run(tt.name, func(t *testing.T) {switchDemo(tt.args.inputNum)})}
}func Test_mutSwitchDemo(t *testing.T) {type args struct {aChar byte}tests := []struct {name stringargs args}{{name: "a",args: args{aChar: 'a'},},{name: "z",args: args{aChar: 'z'},},}for _, tt := range tests {t.Run(tt.name, func(t *testing.T) {mutSwitchDemo(tt.args.aChar)})}
}func Test_noSwitchDemo(t *testing.T) {type args struct {inputNum int}tests := []struct {name stringargs args}{{name: "Negative",args: args{inputNum: -100},},{name: "zero",args: args{inputNum: 0},},{name: "Positive",args: args{inputNum: 100},},}for _, tt := range tests {t.Run(tt.name, func(t *testing.T) {noSwitchDemo(tt.args.inputNum)})}
}func Test_fullThroughDemo(t *testing.T) {type args struct {inputNum int}tests := []struct {name stringargs args}{{name: "1",args: args{inputNum: 1},},}for _, tt := range tests {t.Run(tt.name, func(t *testing.T) {fullThroughDemo(tt.args.inputNum)})}
}func Test_typeSwitchDemo(t *testing.T) {type args struct {i interface{}}tests := []struct {name stringargs args}{{name: "bool",args: args{i: true},},{name: "int",args: args{i: 1},},{name: "string",args: args{i: "hello"},},}for _, tt := range tests {t.Run(tt.name, func(t *testing.T) {typeSwitchDemo(tt.args.i)})}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com