
Go 学习笔记(62)— Go 中 switch 语句中的 switch 表达式和 case 表达式之间的关系
发布日期:2021-05-10 08:44:37
浏览次数:18
分类:精选文章
本文共 3852 字,大约阅读时间需要 12 分钟。
Go���������switch������������������������������������������������������������������������������������������������������������������������Goswitch���������������case���������������������������������������������
switch���������case���������������
- ���������������
package mainimport "fmt"func main() { value1 := [...]int8{0, 1, 2, 3, 4, 5, 6} switch 1 + 3 { case value1[0], value1[1]: fmt.Println("0 or 1") case value1[2], value1[3]: fmt.Println("2 or 3") case value1[4], value1[5], value1[6]: fmt.Println("4 or 5 or 6") }}
- ���������
invalid case value1[0] in switch on 1 + 3 (mismatched types int8 and int)invalid case value1[1] in switch on 1 + 3 (mismatched types int8 and int)
- ���������
- ���������
- switch���������7���������������4������������������������int���
- case������������������������int8���������������������������������
- ���������������
- ������switch������������case���������������������������������
package mainimport "fmt"func main() { value1 := [...]int8{0, 1, 2, 3, 4, 5, 6} switch val := 1 + 3 { case int8(val): // ������������������������ ... }}
- ������switch������������case���������������������������������
switch���������case���������������������
- ���������������
package mainimport "fmt"func main() { value2 := [...]int8{0, 1, 2, 3, 4, 5, 6} switch value2[4] { case 0, 1: fmt.Println("0 or 1") case 2, 3: fmt.Println("2 or 3") case 4, 5, 6: fmt.Println("4 or 5 or 6") }}
- ���������������
- ������case������������������������������������������������������������switch���������
- ���������������������������������������������������������
- ���������������
case������������������������
- ���������������
package mainimport "fmt"func main() { value3 := [...]int8{0, 1, 2, 3, 4, 5, 6} switch value3[4] { case 0, 1, 2: fmt.Println("0 or 1 or 2") case 2, 3, 4: fmt.Println("2 or 3 or 4") case 4, 5, 6: fmt.Println("4 or 5 or 6") }}
- ���������
duplicate case 2 in switchduplicate case 4 in switch
- ���������
- ������case������������������������������������������������������
- ���������
- ���������������
- ������������������������
switch value3[4] { case 0, 1, 2: ... case 4: ... // ���������������case������������ case 5, 6: ...}
- ������������������������
case���������������������������������������
- ���������������
package mainimport "fmt"func main() { value5 := [...]int8{0, 1, 2, 3, 4, 5, 6} switch value5[4] { case value5[0], value5[1], value5[2]: fmt.Println("0 or 1 or 2") case value5[2], value5[3], value5[4]: fmt.Println("2 or 3 or 4") case value5[4], value5[5], value5[6]: fmt.Println("4 or 5 or 6") }}
- ���������������
- ������������������������������������������������������������������
- ������������������������������������
- ���������������������
case include sub expressions that can be the same valueswitch val: case val == a: ... case val == b: ...
- ���������������
������switch���������������������������������
- ���������������
package mainimport "fmt"func main() { value6 := interface{ (byte(127)) } switch t := value6.(type) { case uint8, uint16: fmt.Println("uint8 or uint16") case byte: fmt.Printf("byte") default: fmt.Printf("unsupported type: %T", t) }}
- ���������
duplicate case byte in type switch
- ���������
- ���������
- byte���uint8���������������������������������switch������������������
- ���������������
- ���������������������
case uint8: ...case byte: // ���uint8������������������������
- ������������case������������������������������������������������������������
- ���������������������
������������and������������������������������������������Go������switch���������������������������������������������������������������
发表评论
最新留言
不错!
[***.144.177.141]2025年04月23日 19时38分31秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
上周热点回顾(1.23-1.29)
2021-05-09
Python 简明教程 --- 20,Python 类中的属性与方法
2021-05-09
稀疏数组
2021-05-09
83. Remove Duplicates from Sorted List
2021-05-09
Nmap扫描工具介绍
2021-05-09
玩玩小爬虫——试搭小架构
2021-05-09
Sql Server之旅——第十站 看看DML操作对索引的影响
2021-05-09
Python大神编程常用4大工具,你用过几个?
2021-05-09
centos7一步一步搭建docker jenkins 及自定义访问路径重点讲解
2021-05-09
【Flink】Flink 底层RPC框架分析
2021-05-09
MySQL错误日志(Error Log)
2021-05-09
oracle使用DBMS_RANDOM包生成随机数据
2021-05-09
C++高精度模板
2021-05-09
解决:angularjs radio默认选中失效问题
2021-05-09
windows环境下安装zookeeper(仅本地使用)
2021-05-09
缓冲区溢出实例(一)--Windows
2021-05-09