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

相关推荐
java1234_小锋30 分钟前
[免费]基于Python的Flask酒店客房管理系统【论文+源码+SQL脚本】
开发语言·人工智能·python·flask·酒店客房
bubiyoushang88843 分钟前
使用MATLAB计算梁单元的刚度矩阵和质量矩阵
开发语言·matlab·矩阵
三掌柜6661 小时前
C++ 零基础入门与冒泡排序深度实现
java·开发语言·c++
码事漫谈1 小时前
从一个问题深入解析C++字符串处理中的栈损坏
后端
Yyyy4821 小时前
标签Labels、Scheduler:调度器、k8s污点与容忍度
开发语言·kubernetes
来来走走1 小时前
Android开发(Kotlin) 扩展函数和运算符重载
android·开发语言·kotlin
码事漫谈1 小时前
C++ 核心基石:深入理解 RAII 思想,告别资源泄露的噩梦
后端
zz-zjx1 小时前
云原生LVS+Keepalived高可用方案(二)
开发语言·php·lvs
wuwu_q1 小时前
用通俗易懂 + Android 开发实战的方式,详细讲解 Kotlin Flow 中的 retryWhen 操作符
android·开发语言·kotlin
Mos_x1 小时前
使用Docker构建Node.js应用的详细指南
java·后端