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

相关推荐
南部余额1 分钟前
Python 类变量与实例变量完全指南:区别、使用场景及常见陷阱
开发语言·python
珊瑚里的鱼33 分钟前
第九讲 | 模板进阶
开发语言·c++·笔记·visualstudio·学习方法·visual studio
未来之窗软件服务42 分钟前
人体肢体渲染-一步几个脚印从头设计数字生命——仙盟创梦IDE
开发语言·ide·人工智能·python·pygame·仙盟创梦ide
Echo``1 小时前
40:相机与镜头选型
开发语言·人工智能·深度学习·计算机视觉·视觉检测
noravinsc1 小时前
InforSuite RDS 与django结合
后端·python·django
lisw051 小时前
R语言的专业网站top5推荐
开发语言·r语言
清同趣科研1 小时前
扩增子分析|R分析之微生物生态网络稳定性评估之节点和连接的恒常性、节点持久性以及组成稳定性指数计算
开发语言·r语言
纨妙1 小时前
python打卡打印26
开发语言·python
.小墨迹2 小时前
Apollo学习——键盘控制速度
linux·开发语言·c++·python·学习·计算机外设
Brookty2 小时前
【MySQL】基础知识
后端·学习·mysql