golang中,一般strcut包含 interface类型后,struct类型都需要实现 interface导出的接口,从而成为相应的 interface接口类。
实际上,struct包含interface之后,并不需要实现interface的接口,也能成为 interface接口类。
代码如下:
type newEr interface { New() } type testInterface interface { newEr Done() <-chan struct{} } type kkTest struct { testInterface } func NewTest() newEr { return kkTest{} } func main() { kk := NewTest() i,ok := kk.(testInterface) fmt.Println(i,ok) ch := i.Done() fmt.Println(ch) }
其中 i,ok := kk.(testInterface) 测试成功,也就是说 kkTest 已经是 testInterface 接口类,但是后续 ch := i.Done() 引发 panic,这个也是预料之内的。
相关的应用可以看 context包中的实现,valueCtx部分实现了 Context 接口函数,对其不需要的函数没有实现,如果调用了这些未实现的函数就会导致 panic。这样在程序排错其实是很有好处的,因为调用到这些接口,说明代码其实已经写错了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持小牛知识库。
一直认为implements只能实现interface,今天看到某个开源项目,prisma+nest,通过prisma的类型来定义entity,发现type也能实现,type不是仅仅是一个类型别名吗? !
TypeScript 如何约束一个 interface ,其中的两个值为2选一, (不能都存在,也不能都不存在) 如下: 这个 interface 目前并不符合我的需求,我的需求是 name 或者 nickName 二选一,该如何改造?或者如何实现呢?
本文向大家介绍MyBatis实现动态SQL的实现方法,包括了MyBatis实现动态SQL的实现方法的使用技巧和注意事项,需要的朋友参考一下 MyBatis 最强大的特性之一就是它的动态语句功能。如果您以前有使用JDBC或者类似框架的 经历,您就会明白把SQL语句条件连接在一起是多么的痛苦,要确保不能忘记空格或者不要在 columns列后面省略一个逗号等。动态语句能够完全解决掉这些痛苦。 尽管与动
interface Go语言里面设计最精妙的应该算interface,它让面向对象,内容组织实现非常的方便,当你看完这一章,你就会被interface的巧妙设计所折服。 什么是interface 简单的说,interface是一组method的组合,我们通过interface来定义对象的一组行为。 我们前面一章最后一个例子中Student和Employee都能SayHi,虽然他们的内部实现不一样,
描述: 这是别人可以实现的一个接口。 版本: '>=3.3.0' Syntax(语法) 用作JSDoc 标签字典 (默认开启): @interface [<name>] 用作Closure Compiler 标签字典: @interface Overview @interface标签使一个标识符作为其他标识符的一个实现接口。 例如,你的代码可能定义一个父类,它的方法和属性被去掉。您可以将@inte
GraphQL Interfaces are a sort of "parent object" from which other objects can "inherit" from. For example, Stars is considered an interface, because both Repository and Gist can be starred. An interfa