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 分钟前
从0开始搭建web应用
后端
Ray664 分钟前
guide-rpc-framework笔记(四):网络编程
后端
Moonbit12 分钟前
安装Wasm版的MoonBit工具链
后端·node.js·webassembly
法欧特斯卡雷特18 分钟前
从 Kotlin 编译器 API 的变化开始: 2.2.2X -> 2.3.0-Beta1
后端·架构·开源
zjjuejin23 分钟前
Maven 现代开发流程的集成
java·后端·maven
lly20240624 分钟前
AJAX JSON 实例
开发语言
QiZhang | UESTC34 分钟前
JAVA算法练习题day27
java·开发语言·c++·算法·leetcode·hot100
hrrrrb35 分钟前
【Spring Boot】Spring Boot 中常见的加密方案
java·spring boot·后端
坚持就完事了37 分钟前
2-C语言中的数据类型
c语言·开发语言
Lilian38 分钟前
Trae通过ssh远程访问服务器linux系统不兼容问题
后端·trae