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

相关推荐
小刀飘逸5 分钟前
部署go项目到linux服务器(简易版)
后端·go
Yharim6 分钟前
ruoyi-cloud起步、新增模块和定制
后端·微服务
红狐寻道8 分钟前
osgUtil::Simplifier 实践
c++·后端
我是唐青枫17 分钟前
如何用Go写一个benchmark 解析器及Web UI 数据可视化?
golang
Clocky718 分钟前
opencv-python基础
开发语言·python
Zz_waiting.33 分钟前
多线程进阶
java·开发语言·jvm·javaee
你们补药再卷啦40 分钟前
集成nacos2.2.1出现的错误汇总
java·后端·spring
满怀101542 分钟前
【Python Requests 库详解】
开发语言·python
布道谷1 小时前
关于CAS(自旋锁)的深度解析
后端
快乐源泉1 小时前
【设计模式】啊?没听过命令模式,有什么优点?
后端·设计模式