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

相关推荐
learning-striving8 分钟前
django模型、项目、配置、模型类、数据库操作、查询、F/Q对象、字段类型、聚合函数、排序函数
数据库·后端·python·mysql·oracle·django·sqlite
捕风捉你30 分钟前
深入理解适配器模式:Java实现与框架应用
java·开发语言·适配器模式
JAVA开发区39 分钟前
Python测试框架 pytest : 从零开始的完全指南
开发语言·python·pytest
阑梦清川40 分钟前
数学建模--Matlab操作与运算
开发语言·数学建模·matlab
惊鸿一博1 小时前
python_时间戳对齐
开发语言·python
bryant_meng1 小时前
【python】OpenCV—Aruco
开发语言·python·opencv·aruco
武子康1 小时前
Hadoop-01-基础环境搭建 超详细 Hadoop Java 环境变量 3节点云服务器 2C4G XML 集群配置 HDFS Yarn MapRedece
xml·java·大数据·服务器·hadoop·后端·hdfs
盘门1 小时前
华为OD机考题HJ24 合唱队
java·开发语言
qq8368695201 小时前
Spring Boot与Kafka的集成应用
spring boot·后端·kafka
人类群星闪耀时1 小时前
使用Python实现深度学习模型:策略梯度方法
开发语言·python·深度学习