dtu15253 2017-06-08 06:53
浏览 51
已采纳

仅导出由嵌入式结构实现的方法子集

Is it possible to export only a subset of methods implemented by an embedded struct? Is this a very go-unlike way to reduce copy - and - pasting of code and there is a more idiomatic way to do this?

type A struct {
}

func (a *A) Hello() {
    fmt.Println("Hello!")
}

func (a *A) World() {
    fmt.Println("World!")
}

type B struct {
    A
}

type C struct {
    A
}

func main() {
    b := B{}
    c := C{}

    // B should only export the Hello - function
    b.Hello()

    // C should export both Hello - and World - function
    c.Hello()
    c.World()
}

展开全部

  • 写回答

1条回答 默认 最新

  • dsf5632 2017-06-08 06:57
    关注

    This is how embedding works, there's nothing you can do about it. (Actually there is, see dirty trick at the end.)

    What you want may be achieved with interfaces though. Make your structs unexported, (B => b and C => c), and create "constructor" like functions, which return interface types, containing only the methods you wish to publish:

    type b struct {
        A
    }
    
    type c struct {
        A
    }
    
    type Helloer interface {
        Hello()
    }
    
    type HelloWorlder interface {
        Helloer
        World()
    }
    
    func NewB() Helloer {
        return &b{}
    }
    
    func NewC() HelloWorlder {
        return &c{}
    }
    

    You might want to call the interfaces and functions different, this is just for demonstration.

    Also note that while the returned Helloer interface does not include the World() method, it is still possible to "reach" it using type assertion, e.g.:

    h := NewB() // h is of type Helloer
    if hw, ok := h.(HelloWorlder); ok {
        hw.World() // This will succeed with the above implementations
    }
    

    Try this on the Go Playground.

    Dirty trick

    If a type embeds the type A, (fields and) methods of A that get promoted will become part of the method set of the embedder type (and thus become methods of type A). This is detailed in Spec: Struct types:

    A field or method f of an anonymous field in a struct x is called promoted if x.f is a legal selector that denotes that field or method f.

    The focus is on the promotion, for which the selector must be legal. Spec: Selectors describes how x.f is resolved:

    The following rules apply to selectors:

    1. For a value x of type T or *T where T is not a pointer or interface type, x.f denotes the field or method at the shallowest depth in T where there is such an f. If there is not exactly one f with shallowest depth, the selector expression is illegal.

    [...]

    What does this mean? Simply by embedding, B.World will denote the B.A.World method as that is at the shallowest depth. But if we can achieve so that B.A.World won't be the shallowest, the type B won't have this World() method, because B.A.World won't get promoted.

    How can we achieve that? We may add a field with name World:

    type B struct {
        A
        World int
    }
    

    This B type (or rather *B) will not have a World() method, as B.World denotes the field and not B.A.World as the former is at the shallowest depth. Try this on the Go Playground.

    Again, this does not prevent anyone to explicitly refer to B.A.World(), so that method can be "reached" and called, all we achieved is that the type B or *B does not have a World() method.

    Another variant of this "dirty trick" is to exploit the end of the first rule: "If there is not exactly one f with shallowest depth". This can be achieved to also embed another type, another struct which also has a World field or method, e.g.:

    type hideWorld struct{ World int }
    
    type B struct {
        A
        hideWorld
    }
    

    Try this variant on the Go Playground.

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥30 SD中的一段Unet下采样代码其中的resnet是谁跟谁进行残差连接
  • ¥15 Unet采样阶段的res_samples问题
  • ¥60 Python+pygame坦克大战游戏开发实验报告
  • ¥15 R语言regionNames()和demomap()无法选中中文地区的问题
  • ¥15 Open GL ES 的使用
  • ¥15 我如果只想表示节点的结构信息,使用GCN方法不进行训练可以吗
  • ¥15 QT6将音频采样数据转PCM
  • ¥15 下面三个文件分别是OFDM波形的数据,我的思路公式和我写的成像算法代码,有没有人能帮我改一改,如何解决?
  • ¥15 Ubuntu打开gazebo模型调不出来,如何解决?
  • ¥100 有chang请一位会arm和dsp的朋友解读一个工程
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部