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

相关推荐
_OP_CHEN2 分钟前
【从零开始的Qt开发指南】(九)Qt 常用控件之显示类控件(下):ProgressBar 与 CalendarWidget 实战进阶
开发语言·c++·qt·gui·前端开发·图形化界面开发·qt常用控件
oioihoii3 分钟前
VS Code终端从入门到精通完全指南
开发语言
是一个Bug3 分钟前
声明式事务
java·开发语言·面试
武藤一雄3 分钟前
C#:深入浅出委托(Delegate/Func/Action/Predicate)
开发语言·后端·microsoft·微软·c#·.net
2501_941982056 分钟前
RPA 赋能企业微信外部群:多群同步操作的技术实现
java·开发语言
编程修仙8 分钟前
第六篇 HttpServletRequest对象
java·spring boot·后端
忆~遂愿9 分钟前
vLLM Ascend 项目架构解析与部署配置指南
人工智能·后端·python·ai
sugar椰子皮18 分钟前
【爬虫框架-5】实现一下之前的思路
开发语言·爬虫·python
沈询-阿里19 分钟前
AI Agent系列 - 1 什么是 ReAct Agent?
开发语言·javascript·ecmascript
闲人编程22 分钟前
Flask扩展开发:从零编写自己的Flask扩展
后端·python·flask·sqlalchemy·config·login·codecapsule