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

相关推荐
rit84324991 小时前
基于MATLAB的模糊图像复原
开发语言·matlab
fie88891 小时前
基于MATLAB的声呐图像特征提取与显示
开发语言·人工智能
程序员小凯1 小时前
Spring Boot测试框架详解
java·spring boot·后端
_extraordinary_2 小时前
Java SpringMVC(二) --- 响应,综合性练习
java·开发语言
你的人类朋友2 小时前
什么是断言?
前端·后端·安全
@。1243 小时前
对于灰度发布(金丝雀发布)的了解
开发语言·前端
程序员小凯3 小时前
Spring Boot缓存机制详解
spring boot·后端·缓存
i学长的猫3 小时前
Ruby on Rails 从0 开始入门到进阶到高级 - 10分钟速通版
后端·ruby on rails·ruby
用户21411832636024 小时前
别再为 Claude 付费!Codex + 免费模型 + cc-switch,多场景 AI 编程全搞定
后端
hsjkdhs4 小时前
C++之多层继承、多源继承、菱形继承
开发语言·c++·算法