drmet46444 2018-04-22 05:10
浏览 78
已采纳

GoLang的类型转换到由结构体和嵌入式结构体实现的接口的工作方式

I recently came across a code that is doing something I don't understand.

There are multiple structs having the same embedded struct and an interface that defines methods returning pointer to each struct. This interface is implemented by the embedded struct but only 'partially' by the individual structs, as such, each struct only implements the method where the pointer to that struct is returned.

For better understanding, here is the representative code:

type BarStocks interface {
    GetVodka() *Vodka
    GetMartini() *Martini
    GetBourbon() *Bourbon
    GetNegroni() *Negroni
    GetManhattan() *Manhattan
}

type BaseAttributes struct {
    ID        uuid.UUID
    Quantity float64
    CreatedAt time.Time
    UpdatedAt time.Time
}

func (e *BaseAttributes) GetVodka() *Vodka {
    return nil
}

func (e *BaseAttributes) GetMartini() *Martini {
    return nil
}

func (e *BaseAttributes) GetBourbon() *Bourbon {
    return nil
}

func (e *BaseAttributes) GetNegroni() *Negroni {
    return nil
}

func (e *BaseAttributes) GetManhattan() *Manhattan {
    return nil
}

And then each individual struct implements only the method where its pointer is returned, for example:

type Vodka struct {
    BaseAttributes

    Label string
}

func (v *Vodka) GetVodka() *Vodka {
    return v
}

Now in the code, this setup is used to typecast the individual struct to the interface as a pointer, something like this:

func someFunc() BarStocks {
    v := Vodka{}
    return &v
}

Now I am not too deep into Go yet and so unable to comprehend how the pointer to the struct becomes the same type as the interface.

Thanks in advance for any insight into this.

  • 写回答

2条回答 默认 最新

  • dongqie4233 2018-04-22 05:38
    关注

    I'll do my best to answer the question I think you're asking.

    The documentation on embedding explains the behavior you're seeing,

    There's an important way in which embedding differs from subclassing. When we embed a type, the methods of that type become methods of the outer type, but when they are invoked the receiver of the method is the inner type, not the outer one.

    This explains how a Vodka struct, which embeds struct BaseAttributes which implements all of the methods in BarStocks is able to satisfy the interface Barstocks. This excerpt, however, does not explain how we effectively override GetVodka() for our Vodka struct.

    To understand this we need to read another excerpt from the documentation.

    Embedding types introduces the problem of name conflicts but the rules to resolve them are simple. First, a field or method X hides any other item X in a more deeply nested part of the type.

    This excerpt explains that If Vodka implements GetVodka() and embeds a struct (BaseAttributes) which also implements GetVodka(), the outer-most definition is the one that takes precedence.

    The combination of these behaviors explain how Vodka satisfies the BarStocks interface and has the behavior you see in the example code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么