type switch

type switch的基本用法

type switch是Go语言中一种特殊的switch语句,它比较的是类型而不是具体的值。它判断某个接口变量的类型,然后根据具体类型再做相应处理。注意,在type switch语句的case子句中不能使用fallthrough

用法如下:

switch x.(type) {
case Type1:
    doSomeThingWithType1()
case Type2:
    doSomeThingWithType2()
default:
    doSomeDefaultThing()
}

其中,x必须是一个接口类型的变量,而所有的case语句后面跟的类型必须都实现了接口x。

为了便于理解,我们可以结合下面这个例子来看:

package main

import (
    "fmt"
)

type Animal interface {
    shout() string
}

type Dog struct {}

func (self Dog) shout() string {
    return fmt.Sprintf("wang wang")
}

type Cat struct {}

func (self Cat) shout() string {
    return fmt.Sprintf("miao miao")
}

func main() {
    var animal Animal = Dog{}

    switch animal.(type) {
    case Dog:
        fmt.Println("animal'type is Dog")
    case Cat:
        fmt.Println("animal'type is Cat")
    }
}

在上面的例子中,Cat和Dog类型都实现了接口Animal,所以它们可以跟在case语句后面,判断接口变量animal是否是对应的类型。

在switch中声明变量

如果不仅想要判断某个接口变量的类型,还想要获得其类型转换后的值的话,我们可以在switch的语句表达式中声明一个变量来获得这个值:

package main

import (
	"fmt"
	"reflect"
)

type Animal interface {
	shout() string
}

type Dog struct {
	name string
}

func (self Dog) shout() string {
	return fmt.Sprintf("wang wang")
}

type Cat struct {
	name string
}

func (self Cat) shout() string {
	return fmt.Sprintf("miao miao")
}

type Tiger struct {
	name string
}

func (self Tiger) shout() string {
	return fmt.Sprintf("hou hou")
}

type Wolf struct {
	name string
}

func (self Wolf) shout() string {
	return fmt.Sprintf("ao ao")
}

func main() {
	// var animal Animal // 验证case nil
	var animal Animal = Dog{"Harry"}
	// var animal Animal = Tiger{"Woods"}
	// var animal Animal = Wolf{} // 验证default

	switch a := animal.(type) {
	// a的类型是 Animal
	case nil:
		fmt.Println("nil", a)

	// a的类型是Animal
	case Dog, Cat:
		fmt.Println(a)
		// 这里会报错,因为Animal类型没有成员name
		// fmt.Println(a.name)

	// a的类型是Tiger
	case Tiger:
		fmt.Println(a.shout(), a.name)

	// a的类型是Animal
	default:
		fmt.Println("default", reflect.TypeOf(a), a)
	}
}

在上述代码中,我们可以看到a := animal.(type)语句隐式地为每个case子句声明了一个变量a。

变量a类型的判定规则如下:

  • 如果case后面跟着一个类型,那么变量a在这个case子句中就是这个类型。例如在case Tiger子句中a的类型就是Tiger。
  • 如果case后面跟着多个类型,那么变量a的类型就是接口变量animal的类型,例如在case Dog, Cat子句中a的类型就是Animal。
  • 如果case后面跟着nil,那么变量a的类型就是接口变量animal的类型Animal,通常这种子句用来判断未赋值的接口变量。
  • default子句中变量a的类型是接口变量animal的类型

参考

Type switches

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值