go switch 与 interface

go switch 与 interface

前言

github.com/google/cel-go/common/types/ref

go 复制代码
type Val interface {
	// ConvertToNative converts the Value to a native Go struct according to the
	// reflected type description, or error if the conversion is not feasible.
	ConvertToNative(typeDesc reflect.Type) (interface{}, error)

	// ConvertToType supports type conversions between value types supported by
	// the expression language.
	ConvertToType(typeValue Type) Val

	// Equal returns true if the `other` value has the same type and content as
	// the implementing struct.
	Equal(other Val) Val

	// Type returns the TypeValue of the value.
	Type() Type

	// Value returns the raw value of the instance which may not be directly
	// compatible with the expression language types.
	Value() interface{}
}

当我们实现了一个interface时,使用swtich case去判断,会命中哪条结果?

go 复制代码
switch i := v.(type) {
	case ref.Val:
		return resolveType(i.Value())
	case *CustomType:
		return resolveType(i.Value())
	default:
		
	}

经过测试发现,谁的case靠前,会命中谁,哪怕CustomType有一些私有方法

后面空了看看switch 底层实现。。

相关推荐
不吃香菜学java6 小时前
Redis的java客户端
java·开发语言·spring boot·redis·缓存
码事漫谈6 小时前
大模型输出的“隐性结构塌缩”问题及对策
前端·后端
小江的记录本7 小时前
【网络安全】《网络安全常见攻击与防御》(附:《六大攻击核心特性横向对比表》)
java·网络·人工智能·后端·python·安全·web安全
贵沫末7 小时前
python——打包自己的库并安装
开发语言·windows·python
文祐7 小时前
C++类之虚函数表及其内存布局(一个子类继承一个父类)
开发语言·c++
努力的小雨7 小时前
龙虾量化实战法(QClaw)
后端
zuowei28897 小时前
华为网络设备配置文件备份与恢复(上传、下载、导出,导入)
开发语言·华为·php
橙露7 小时前
SpringBoot 整合 MinIO:分布式文件存储上传下载
spring boot·分布式·后端
xiaohe077 小时前
超详细 Python 爬虫指南
开发语言·爬虫·python
嗑嗑嗑瓜子的猫8 小时前
Java!它值得!
java·开发语言