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 MATLAB卫星二体模型仿真
  • ¥15 怎么让数码管亮的同时让led执行流水灯代码
  • ¥20 SAP HANA SQL Script 。如何判断字段值包含某个字符串
  • ¥85 cmd批处理参数如果含有双引号,该如何传入?
  • ¥15 fx2n系列plc的自控成型机模拟
  • ¥15 时间序列LSTM模型归回预测代码问题
  • ¥50 使用CUDA如何高效的做并行化处理,是否可以多个分段同时进行匹配计算处理?目前数据传输速度有些慢,如何提高速度,使用gdrcopy是否可行?请给出具体意见。
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),如何通过环岛的原理及完整代码
  • ¥20 机器学习或深度学习问题?困扰了我一个世纪,晚来天欲雪,能饮一杯无?
  • ¥15 c语言数据结构高铁订票系统