Go语言学习之Switch语句的使用

作者:孙琦Ray 时间:2024-04-23 09:38:57 

基本语法

在讲述if-else时已经提到,如果有多个判断条件,Go语言中提供了Switch-Case的方式。如果switch后面不带条件相当于switch true

// Convert hexadecimal character to an int value
switch {
case '0' <= c && c <= '9':
    return c - '0'
case 'a' <= c && c <= 'f':
    return c - 'a' + 10
case 'A' <= c && c <= 'F':
    return c - 'A' + 10
}
return 0

fallthrough使用方法

默认情况下,case满足执行后会进行break,后面case即使满足条件也不再循环,如果想继续执行,则需要添加fallthrough,

package main

import "fmt"

func main() {
   i := 3
   switch i {
   case i > 0:
       fmt.Println("condition 1 triggered")
       fallthrough
   case i > 2:
       fmt.Println("condition 2 triggered")
       fallthrough
   default:
       fmt.Println("Default triggered")
   }
}

此时所有的case都会被执行

condition 1 triggered
condition 2 triggered
Default triggered

多条件匹配

如果同一个条件满足,也可以这样罗列到同一条件,相当于或条件

switch i {
   case 0, 1:
       f()
   default:
       g()
}

判断接口(interface)类型

空接口

后面我们会讲到接口,通过switch可以对type进行判断,获取接口的真实类型。

package main

import "fmt"

func main() {
   var value interface{}
   switch q:= value.(type) {
      case bool:
      fmt.Println("value is of boolean type")
      case float64:
      fmt.Println("value is of float64 type")
      case int:
      fmt.Println("value is of int type")
      default:
      fmt.Printf("value is of type: %T", q)
  }
}

在上面的例子中,我们定义了一个空接口

var value interface{}

同时使用switch来判断类型

switch q:= value.(type) {

由于空接口没有内容,所以类型为nil,触发了default

value is of type: <nil>

获取实际类型

我们对上面的例子进行改造,同时让空接口拥有实际的值,再来看看执行的效果

package main

import "fmt"

func valueType(i interface{}) {
   switch q:= i.(type) {
      case bool:
      fmt.Println("value is of boolean type")
      case float64:
      fmt.Println("value is of float64 type")
      case int:
      fmt.Println("value is of int type")
      default:
      fmt.Printf("value is of type: %T\n", q)

}
}

func main() {
   person := make(map[string]interface{}, 0)

person["name"] = "Alice"
   person["age"] = 21
   person["height"] = 167.64

fmt.Printf("%+v\n", person)

for _, value := range person {
       valueType(value)
   }
}

这里有几个还没有讲到的知识点:

  • 函数的定义方法

  • 定义了一个map,但是值的类型为空接口,意思就是可以是任何类型的值,这在接口章节还会详细讲解,所以大家看到这里不要纠结,继续往下看

  • 赋值时,特意给value不同的类型, string/int/float类型

最后通过循环将变量传给valueType函数,看看程序输出什么结果

map[age:21 height:167.64 name:Alice]
value is of type: string
value is of int type
value is of float64 type

来源:https://blog.csdn.net/xiaoquqi/article/details/125532907

标签:Go,Switch
0
投稿

猜你喜欢

  • 基于python实现获取网页图片过程解析

    2023-10-08 21:57:59
  • 一篇文章入门Python生态系统(Python新手入门指导)

    2023-11-03 01:12:00
  • python保存数据到本地文件的方法

    2023-10-01 20:59:53
  • python发送邮件脚本

    2022-01-28 03:04:58
  • conda与jupyter notebook kernel核环境不一致的问题解决

    2021-07-03 15:43:02
  • mysql 一个较特殊的问题:You can't specify target table 'wms_cabinet_form'

    2024-01-24 19:07:13
  • Js setInterval与setTimeout(定时执行与循环执行)的代码(可以传入参数)

    2024-04-16 09:23:29
  • 如何把IP表存到SQL数据库里去?

    2009-11-02 20:21:00
  • IE8 CSS之生成内容

    2008-09-09 22:14:00
  • Python最大连续区间和动态规划

    2023-02-13 09:40:32
  • python开启摄像头以及深度学习实现目标检测方法

    2023-10-27 03:23:18
  • 用DIV+CSS编写出的网页中的圆角矩形

    2007-10-14 16:03:00
  • Django ORM 查询管理器源码解析

    2023-01-30 12:28:47
  • Numpy数组转置的实现

    2022-10-16 09:15:34
  • 用python做一个搜索引擎(Pylucene)的实例代码

    2023-06-30 22:12:40
  • 完整java开发中JDBC连接数据库代码和步骤

    2024-01-21 19:27:23
  • js实现鼠标切换图片(无定时器)

    2023-09-07 02:44:58
  • python3中替换python2中cmp函数的实现

    2021-08-15 01:42:41
  • 基于tensorflow for循环 while循环案例

    2022-01-26 14:40:34
  • Python版微信红包分配算法

    2022-04-10 12:48:22
  • asp之家 网络编程 m.aspxhome.com