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

相关推荐
Python私教15 分钟前
Rust基本语法
后端
张np16 分钟前
java基础-集合接口(Collection)
java·开发语言
Python私教16 分钟前
Rust环境搭建
后端
jakeswang20 分钟前
ServletLess架构简介
java·后端·servletless
开始了码24 分钟前
QT:ItemWidgets模块介绍
开发语言·qt
汪汪队立大功12327 分钟前
JavaScript是怎么和html元素关联起来的?
开发语言·javascript·html
Mr_Dwj1 小时前
【Python】Python 基本概念
开发语言·人工智能·python·大模型·编程语言
夕颜1111 小时前
如何让 AI 按照你的预期输出
后端
q***56381 小时前
Spring Boot--@PathVariable、@RequestParam、@RequestBody
java·spring boot·后端