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

相关推荐
卷无止境2 分钟前
pytest 从零到实战:要想代码好,测试少不了
后端·python
K神6 分钟前
Spring gRPC集成Spring Security OAuth2
后端·grpc
Ai拆代码的曹操7 分钟前
Dubbo 直连模式翻车现场:2.7.x 调 3.x 的序列化大坑
后端
Chenyiax13 分钟前
AC 自动机原理:把一组关键词编译成一次线性扫描
后端
程序员cxuan17 分钟前
读懂 Claude Code 架构分析系列,第二篇:Claude Code 是怎样启动的?
人工智能·后端·程序员
用户409666013175128 分钟前
MySQL 慢查询优化实战:从 EXPLAIN 到索引设计
后端
蜗牛~turbo30 分钟前
金蝶云星空的网络控制设置
开发语言·c#·金蝶·erp·云星空·k3 cloud
从零开始的代码生活_1 小时前
Linux epoll 多路转接详解
linux·运维·网络·后端·tcp/ip·计算机网络·php
Reload.1 小时前
GJ航司,验证码网易易盾 JS逆向 纯算轨迹
开发语言·前端·javascript
IT_陈寒1 小时前
Vue的响应式更新把我坑惨了,原来是这个原因
前端·人工智能·后端