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

相关推荐
沐知全栈开发几秒前
R 列表:深入解析与高效应用
开发语言
程序员小假1 分钟前
我们来说一说 Java 反序列化漏洞
后端
满分观察网友z2 分钟前
别再写成吨的 if-else 了!Java 反射:让你的代码学会“见招拆招”的魔法
后端
爱喝茶的小茶2 分钟前
周赛98补题
开发语言·c++·算法
hai99long3 分钟前
MySQL 的BinLog日志
后端
二闹4 分钟前
SLF4J/Logback 配置与 ELK 集成实战指南
后端·elasticsearch·logstash
福大大架构师每日一题5 分钟前
2025-07-04:统计符合条件长度为 3 的子数组数目。用go语言,给定一个整数数组 nums,请你计算有多少个长度恰好为 3 的连续子数组满足这样的条件:
后端
SimonKing6 分钟前
解锁万能文件内容分析工具:Apache Tika
java·后端·程序员
LaoZhangAI20 分钟前
AI ASMR视频免费制作教程2025:TikTok爆火新趋势详解
前端·后端
LaoZhangAI20 分钟前
FLUX.1 Kontext API完全指南:文本图像编辑最强工具2025版
前端·后端