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