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

相关推荐
源码_空间2 分钟前
基于Springboot的在线错题本与复习计划系统(源码+文档)
java·spring boot·后端
Rain的Java大神之路2 分钟前
别再用“延迟双删”赌一致性:Canal + RocketMQ + Redis 的工业级缓存失效链路,以及 MySQL 分库分表前必须算清的那笔账
java·后端·面试
himobrinehacken14 分钟前
HackMyVM靶机Yulian
java·spring boot·后端
CoderYanger19 分钟前
Java EE 进阶:16. Spring原理(第一弹)
java·服务器·数据库·后端·spring·java-ee·github
Brain 博览记41 分钟前
给吗啡类镇痛药装上“光开关”:照一下开、再照一下关——Cell 冷冻电镜解析阿片效价切换
开发语言·网络·论文阅读·学习·php
拖孩41 分钟前
群里有人开始比「次数」之后,我做了个排行榜小程序
前端·后端·微信小程序
凤年徐42 分钟前
Qt 第三章 常用控件总结
开发语言·qt
陈随易1 小时前
CDN 自带安全防护,SQL 注入、XSS、命令攻击现原形
前端·后端
SunnyDays10111 小时前
Java 裁切 PDF 页面:去除多余白边、批量裁剪与局部图片导出
java·开发语言·pdf
Elastic 中国社区官方博客1 小时前
Elasticsearch 日志写入提速:用 3 项设置和 logsdb_columnar 从每秒 5.2 万行提升到 11.2 万行
大数据·开发语言·elasticsearch·搜索引擎·全文检索