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

相关推荐
Victor3562 分钟前
Redis(170)如何使用Redis实现分布式限流?
后端
Victor3567 分钟前
Redis(171)如何使用Redis实现分布式事务?
后端
Tsonglew8 分钟前
Python 自由线程实现原理深度解析
后端·python
火山灿火山1 小时前
Qt常用控件(二)
开发语言·qt
西游音月1 小时前
(12)功能实现:Qt实战项目之读写配置文件
开发语言·qt
VBA63372 小时前
VBA之Word应用第四章第五节:段落Paragraph对象的属性(一)
开发语言
whltaoin2 小时前
【Java SE】Java IO体系深度剖析:从原理到实战的全方位讲解(包含流操作、序列化与 NIO 优化技巧)
java·开发语言·nio·se·io体系
csbysj20206 小时前
jQuery 删除元素
开发语言
xxy-mm6 小时前
Javascript 中的继承
开发语言·javascript·ecmascript
锋行天下8 小时前
公司内网部署大模型的探索之路
前端·人工智能·后端