是否甚至可能(在Go中没有泛型)“成功地”扩展结构,其方法是链式的? Java中此问题的解决方案是使用棘手的泛型类型(example)。
问题示例(go playground):
- 具有方法链接的基本接口(
A
):
type A interface {
doA() A
// other methods...
finalizeA() error
}
- 派生接口(
B
),它扩展了A
的方法集:
type B interface {
A
doB() B
// other methods...
finalizeB() error
}
func NewB() B {
...
}
- 我想通过以下方式使用派生的实现:
err := NewB().DoA().FinalizeB()
但是DoA()
返回A
,并且A
没有为FinalizeB()
实现。那么,如何在Go中实现派生函数和基本函数的链接?