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

相关推荐
Sunny_yiyi几秒前
Java根据模版导出PDF文件
java·开发语言·pdf
绝无仅有3 分钟前
前端开发环境搭建:从安装 Node 到成功运行代码
后端·面试·github
橘子135 分钟前
C++实战:搜索引擎项目(二)
开发语言·c++·搜索引擎
yshhuang18 分钟前
在Windows上搭建开发环境
前端·后端
绝无仅有20 分钟前
某个互联网大厂的Elasticsearch基础面试题与答案
后端·面试·github
无责任此方_修行中23 分钟前
AWS IoT Core 成本优化实战:从 PoC 到生产的省钱之旅
后端·架构·aws
ITMan彪叔23 分钟前
Java MQTT 主流开发方案对比
java·后端
Cache技术分享24 分钟前
188. Java 异常 - Java 异常处理规范
前端·后端
用户40993225021226 分钟前
如何用 Git Hook 和 CI 流水线为 FastAPI 项目保驾护航?
后端·ai编程·trae