1. package main
  2.  
  3. import (
  4. "fmt"
  5. )
  6.  
  7. func main() {
  8.  
  9. var ar = [...]string{"A", "B", "D", "E"}
  10.  
  11. for _, content := range ar {
  12. switch content {
  13. case "A":
  14. fmt.Println("AAA")
  15. case "B", "C", "D":
  16. fmt.Println("BBB")
  17. default:
  18. fmt.Println("CCC")
  19. }
  20.  
  21. }
  22.  
  23. }

  输出:

AAA

BBB

BBB

CCC

使用fallthrough,来向下一个case语句转移流程控制权,

  1. package main
  2.  
  3. import (
  4. "fmt"
  5. )
  6.  
  7. func main() {
  8.  
  9. var ar = [...]string{"A", "B", "D", "E"}
  10.  
  11. for _, content := range ar {
  12. switch content {
  13. case "A":
  14. fallthrough
  15. case "B", "C", "D":
  16. fmt.Println("BBB")
  17. default:
  18. fmt.Println("CCC")
  19. }
  20.  
  21. }
  22.  
  23. }

  输出:

BBB

BBB

BBB

CCC

类型switch语句:对类型进行判定,而不是值

  1. package main
  2.  
  3. import (
  4. "fmt"
  5. )
  6.  
  7. func main() {
  8.  
  9. v := 11
  10.  
  11. switch i := interface{}(v).(type) {
  12.  
  13. case int, int8, int16, int32, int64:
  14.  
  15. fmt.Printf("A signed integer: %v. the type is :%T\n", v, v)
  16. fmt.Printf("A signed integer: %v. the type is :%T\n", i, i)
  17. case uint, uint8, uint16, uint32, uint64:
  18.  
  19. fmt.Printf("A unsigned integer: %v. the type is :%T\n", v, v)
  20. fmt.Printf("A unsigned integer: %v. the type is :%T\n", i, i)
  21.  
  22. default:
  23.  
  24. fmt.Println("Unknown!")
  25.  
  26. }
  27.  
  28. }

  输出:

A signed integer: 11. the type is :int

A signed integer: 11. the type is :int

  1. package main
  2.  
  3. import (
  4. "fmt"
  5. )
  6.  
  7. func main() {
  8.  
  9. var v interface{}
  10. v = "hi"
  11.  
  12. switch v.(type) {
  13. case string:
  14. fmt.Printf("The string is %v \n", v.(string))
  15.  
  16. case int, int8, int16, int32, int64:
  17.  
  18. fmt.Printf("A signed integer: %v. the type is :%T\n", v, v)
  19.  
  20. case uint, uint8, uint16, uint32, uint64:
  21.  
  22. fmt.Printf("A unsigned integer: %v. the type is :%T\n", v, v)
  23.  
  24. default:
  25.  
  26. fmt.Println("Unknown!")
  27.  
  28. }
  29.  
  30. }

  输出:

The string is hi

go ---switch语句的更多相关文章

  1. switch语句的妙用

    switch语句的普通用法很简单,如下: var a = 3; switch (a) { case 1: console.log(a); break; case 2: case 3: console. ...

  2. 106运用SWITCH语句打印星期几的单词

    package com.chongrui.test;/*运用SWITCH语句打印星期几的单词 * */ public class TypeConvertion { public static void ...

  3. 通过goto语句学习if...else、switch语句并简单优化

    goto语句在C语言中实现的就是无条件跳转,第二章一上来就介绍goto语句就是要通过goto语句来更加清楚直观的了解控制结构. 我理解的goto语句其实跟switch语句有相似之处,都是进行跳转.不同 ...

  4. Java中简单的操作(if语句、常用操作符、switch语句、变量赋值等)

    ---------------------if语句介绍--------------------------------------------------- class IfDemo { public ...

  5. Switch语句的case穿透

    Switch语句的case穿透 一 switch语句几点说明: 1. case后面只能是常量,不能是变量,而且,多个case后面的值不能出现相同的. 2.case后面表达式可以接受: 基本数据类型,b ...

  6. ECMA中的switch语句

    switch借鉴自其他语言,但也有自己的特色. 1.可以在switch语句中使用任何数据类型(数值.字符串.对象等),很多其他语言中只能使用数值. 2.每个case的值不一定是常量,可以是变量或者表达 ...

  7. switch语句下的变量声明和定义

    switch语句下的变量声明和定义的问题: switch...case...语句中存在声明和定义会出现一些问题.这个由switch语法特性决定的, switch中每个case都是平等的层次,区别于一般 ...

  8. 透过IL看C#:switch语句(转)

    透过IL看C# switch语句(上) 摘要: switch语句是 C#中常用的跳转语句,可以根据一个参数的不同取值执行不同的代码.本文介绍了当向 switch语句中传入不同类型的参数时,编译器为其生 ...

  9. switch语句

    应用条件语句可以很方便地使程序实现分支,但是出现分支比较多的时候,虽然可以用嵌套的if语句来解决,但是程序结构会显得复杂,甚至凌乱.为方便实现多情况选择,C++提供了一种switch开关语句.   一 ...

  10. java switch语句注意的事项

    1.switch语句使用的变量只能是byte.char.short.string数据类型. 2.case后面gender数据必须是一个常量. 3.switch的停止条件: switch语句一旦比配上了 ...

随机推荐

  1. GeoIP的使用-C语言版

    0x00. 简介 GeoIP库可以根据IP地址(支持IPv4 和 IPv6), 定位该IP所在的 洲.经纬度.国家.省市.ASN 等信息. GeoIP目前已经升级到GeoIP2,GeoIP2有两个版本 ...

  2. Eclipse properties配置文件中文乱码设置

    1. eclipse中properties的默认编码为  ISO-8859-1, 输入汉字会被转换为unicode 2. 点击  Windows-->preferences  按下图找到更改编码 ...

  3. nginx的 ngx.var ngx.ctx ngx.req

    ngx.var 是获取 Nginx 的变量,需要经历字符串 hash.hash 表查找等过程. ngx.ctx 仅仅是一个 Lua table 而已,它的引用存放在 ngx_lua 的模块上下文(ct ...

  4. Nginx Rewrite相关功能-防盗链

    Nginx Rewrite相关功能-防盗链 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.

  5. DVWA的搭建

    DVWA的搭建 一.DVWA是什么? 一款渗透测试演练系统,俗称靶机. 二.如何搭建? Linux有成套的靶机,直接打开使用就可以,下面开始介绍Windows 下DVWA的搭建. 运行phpstudy ...

  6. Win10开启快速启动后关机变重启

    同样可以用来解决,“msconfig引导为空”,“默认操作系统为空”,“win10改为uefi启动后关机变重启”,“legacy转uefi后无法关机” 问题起因 换完主板和cpu后,新的主板在开机时总 ...

  7. 测试cnblog

    this prelkdfkdjfkljasdlkfjlkjlkjsdf this this code this this pre codeljkjkjkdjkdjkdjkjdkjdkdlajlkdjf ...

  8. WindowChrome

      "chrome"一词在设计术语中是"框架"的意思,即浏览器的除了网页之外的部分. https://www.cnblogs.com/dino623/p/Cus ...

  9. 几款不错的java表达式引擎

    mvel 比较老牌了,很强大,但是好久没更新了 参考地址: http://mvel.documentnode.com/ https://github.com/mvel/mvel ScriptEngin ...

  10. 常用Linux软件安装

    JDK 先从Oracle官网下载JDK Linux版本的安装包,上传到服务器,这里推荐在服务器中创建一个目录/software,可以将所有软件的安装包放在这个目录下(或者是/opt目录下),将软件包解 ...