当前位置: 首页> 健康> 养生 > go switch 与 interface

go switch 与 interface

时间:2025/7/9 11:27:56来源:https://blog.csdn.net/qq_43645782/article/details/139909206 浏览次数:0次

go switch 与 interface

  • 前言

前言

github.com/google/cel-go/common/types/ref

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去判断,会命中哪条结果?

switch i := v.(type) {case ref.Val:return resolveType(i.Value())case *CustomType:return resolveType(i.Value())default:}

经过测试发现,谁的case靠前,会命中谁,哪怕CustomType有一些私有方法
后面空了看看switch 底层实现。。

关键字:go switch 与 interface

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: