如何在Go中扩展流利设计的struct / interface的方法集?

是否甚至可能(在Go中没有泛型)“成功地”扩展结构,其方法是链式的? Java中此问题的解决方案是使用棘手的泛型类型(example)。

问题示例(go playground):

  1. 具有方法链接的基本接口(A):
type A interface {
    doA() A
    // other methods...
    finalizeA() error
}
  1. 派生接口(B),它扩展了A的方法集:
type B interface {
    A
    doB() B
    // other methods...
    finalizeB() error
}

func NewB() B {
    ...
}
  1. 我想通过以下方式使用派生的实现:
err := NewB().DoA().FinalizeB()

但是DoA()返回A,并且A没有为FinalizeB()实现。那么,如何在Go中实现派生函数和基本函数的链接?

huozong 回答:如何在Go中扩展流利设计的struct / interface的方法集?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2922256.html

大家都在问