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

相关推荐
Albert Edison4 小时前
【Python】学生管理系统
开发语言·数据库·python
宇木灵6 小时前
C语言基础-十、文件操作
c语言·开发语言·学习
云泽8087 小时前
C++ 多态入门:虚函数、重写、虚析构及 override/final 实战指南(附腾讯面试题)
开发语言·c++
yanghuashuiyue8 小时前
lambda+sealed+record
java·开发语言
山岚的运维笔记8 小时前
SQL Server笔记 -- 第73章:排序/对行进行排序
数据库·笔记·后端·sql·microsoft·sqlserver
苍何8 小时前
豆包还能这么玩?附 13 大隐藏玩法,效率起飞(建议收藏)
后端
苍何8 小时前
Kimi 版 OpenClaw 来了,5000+ Skills 随便用,确实给力!
后端
yzx9910139 小时前
Python数据结构入门指南:从基础到实践
开发语言·数据结构·python
百锦再9 小时前
Jenkins 全面精通指南:从入门到脚本大师
运维·后端·python·servlet·django·flask·jenkins
衍生星球9 小时前
【JSP程序设计】Servlet对象 — page对象
java·开发语言·servlet·jsp·jsp程序设计