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

相关推荐
周末程序猿24 分钟前
技术总结|OpenSpec 实战:如何先写规格,再让 AI 写代码
后端·ai编程
Brain 博览记1 小时前
脑机接口研究丨FPGA 5 毫秒完成运动想象分类
开发语言·论文阅读·人工智能·机器学习·fpga开发·分类·数据挖掘
计算机–阿拉升学长1 小时前
基于SpringBoot的番茄种植水肥一体化管理系统 — 系统设计与实现
java·spring boot·后端·mysql·vue·javaweb
bai2732 小时前
日志里的 [Object] 不是空对象:console.log 默认只展开两层,console.dir 也一样
javascript·后端
一条Go2 小时前
26 篇之后:Alioth 的 26 个设计决定
后端·github
狗头大军之江苏分军2 小时前
《潮水漫过十七岁》 补一下,运动会
前端·javascript·后端
狗头大军之江苏分军2 小时前
《潮水漫过十七岁》书都带回去
前端·javascript·后端
金銀銅鐵2 小时前
带您看懂字节码指令(六):方法调用里有什么
java·jvm·后端
Brain 博览记3 小时前
脑网络AI丨给图神经网络装上“信息高速路”
开发语言·网络·论文阅读·人工智能·深度学习·神经网络·机器学习
郝学胜-神的一滴3 小时前
Effective Python 条款44:善用属性注解
开发语言·c++·vscode·python·算法·stl