尝试使用go接口实现多态

时间:2018-06-24 08:50:22

标签: go go-interface libchan

我正在尝试在第三方库(在本例中为libchan)上创建一个层。这是我定义的接口:

type ReceiverStream interface {
    Receive(msg interface{}) error
}

type InboundTransport interface {
    WaitReceiveChannel() (ReceiverStream, error)
}

InboundTransportTransport类型的替身:

// libchan.go
type Transport interface {
    // NewSendChannel creates and returns a new send channel.  The receive
    // end will get picked up on the remote end of the transport through
    // the remote calling WaitReceiveChannel.
    NewSendChannel() (Sender, error)

    // WaitReceiveChannel waits for a new channel be created by the
    // remote end of the transport calling NewSendChannel.
    WaitReceiveChannel() (Receiver, error)
}

就上下文而言,这是libchan.Receiver的定义(请注意,它与我的ReceiverStream相匹配:

// libchan.go
type Receiver interface {
    // Receive receives a message sent across the channel from
    // a sender on the other side of the underlying transport.
    // Receive is expected to receive the same object that was
    // sent by the Sender, any differences between the
    // receive and send type should be handled carefully.  It is
    // up to the application to determine type compatibility, if
    // the receive object is incompatible, Receiver will
    // throw an error.
    Receive(message interface{}) error
}

Transport由libchan库here返回:

// libchan/session.go:62
func NewTransport(provider StreamProvider) libchan.Transport {
...
}

由于libchan.TransportInboundTransport共享一种WaitReceiveChannel() (ReceiverStream, error)方法,所以我认为我应该可以将另一个替换为另一个,就像这样:

func (ln SpdyListener) Accept(addr string) InboundTransport {
    var listener net.Listener
    var err error
    listener, err = net.Listen("tcp", addr)
    if err != nil {
        log.Fatal(err)
    }
    c, err := listener.Accept()
    if err != nil {
        log.Fatal(err)
    }
    p, err := spdy.NewSpdyStreamProvider(c, true)
    if err != nil {
        log.Fatal(err)
    }

    return spdy.NewTransport(p)
}

但是我得到一个错误:

cannot use spdy.NewTransport(p) (type libchan.Transport) as type InboundTransport in return argument:
    libchan.Transport does not implement InboundTransport (wrong type for WaitReceiveChannel method)
        have WaitReceiveChannel() (libchan.Receiver, error)
        want WaitReceiveChannel() (ReceiverStream, error)

我认为此错误的意思是ReceiverStream的类型与libchan.Receiver不匹配,但是我认为golang接口是隐式的,这意味着只要返回类型实现相同的方法作为预期的接口,它将通过编译。有什么我可以更改的东西,以便可以将自定义接口叠加到第三方库返回的接口上?

TLDR:第三方库正在返回接口Transport的对象。 Transport接口指定方法WaitReceiveChannel()。我有一个自定义接口InboundTransport,它也指定了WaitReceiveChannel()。我正在调用的第三方方法返回一个通过方法Transport实现WaitReceiveChannel()的对象。我假设它也会实现InboundTransport,因为后者也指定了相同类型的WaitReceiveChannel()。这不起作用。为什么不呢?

1 个答案:

答案 0 :(得分:4)

您已经知道Go中的接口是隐式满足的。

但是,如错误所示,

WaitReceiveChannel() (libchan.Receiver, error)

WaitReceiveChannel() (ReceiverStream, error)

是两种不同的方法类型,导致libchan.Transport不能隐式实现InboundTransport

要解决此问题,您必须在libchan.Transport周围编写一个东西包装器,以正确实现InboundTransport

type TransportWrapper struct {
    t *libchan.Transport
}

func (w *TransportWrapper) WaitReceiveChannel() (Receiver, error) {
    return w.t.WaitReceiveChannel()
}

// ...

func (ln SpdyListener) Accept(addr string) InboundTransport {
    var listener net.Listener
    var err error
    listener, err = net.Listen("tcp", addr)
    if err != nil {
        log.Fatal(err)
    }
    c, err := listener.Accept()
    if err != nil {
        log.Fatal(err)
    }
    p, err := spdy.NewSpdyStreamProvider(c, true)
    if err != nil {
        log.Fatal(err)
    }

    return &TransportWrapper{spdy.NewTransport(p)}
}