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

问题描述 投票:0回答:1

甚至有可能(在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中实现派生函数和基本函数的链接?

go fluent embedding
1个回答
0
投票

不幸的是,如果您非常喜欢Java,似乎没有办法做到这一点

© www.soinside.com 2019 - 2024. All rights reserved.