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 底层实现。。

相关推荐
Yupureki几秒前
《C++实战项目-高并发内存池》4.CentralCache构造
c语言·开发语言·c++·单例模式·github
2401_898075123 分钟前
分布式系统监控工具
开发语言·c++·算法
惊讶的猫4 分钟前
springboot常用注解
java·spring boot·后端
掘金者阿豪6 分钟前
MySQL迁移,别再信“改几行配置就能跑”:金仓KingbaseES深度兼容实战
后端
Cosolar30 分钟前
大模型多轮对话自动上下文压缩
人工智能·后端·面试
APIshop34 分钟前
Java调用亚马逊商品详情API接口完全指南
java·开发语言·python
lsx20240641 分钟前
PostgreSQL中的NULL处理
开发语言
是梦终空1161 小时前
模板编译期机器学习
开发语言·c++·算法
艾莉丝努力练剑1 小时前
文件描述符fd:跨进程共享机制
java·linux·运维·服务器·开发语言·c++
Java面试题总结1 小时前
2026最新Java八股文(完整版)
java·开发语言·jvm·数据库·java面试·java八股文