【问题标题】:Interface as fields in struct and interface calls methods which takes input as same struct接口作为结构中的字段,接口调用将输入作为相同结构的方法
【发布时间】:2019-11-25 06:25:17
【问题描述】:

在我的例子中,“RequestHandlerProxy”是一个结构,其字段作为接口“IAdapter”,接口有可能被调用的方法,该方法的输入作为结构“RequestHandlerProxy”。请帮我解决这个问题?如何定义值来构造“RequestHandlerProxy”并通过?

下面是我的接口结构和方法:接口“IAdapter”在文件“适配器”中

type RequestHandlerProxy struct {
    TestMode       bool
    coreInstanceId string
    adapter        adapters.IAdapter
    coreProxy      adapterif.CoreProxy
}

func NewRequestHandlerProxy(coreInstanceId string, iadapter adapters.IAdapter, cProxy adapterif.CoreProxy) *RequestHandlerProxy {
    var proxy RequestHandlerProxy
    proxy.coreInstanceId = coreInstanceId
    proxy.adapter = iadapter
    proxy.coreProxy = cProxy
    return &proxy
}

func (rhp *RequestHandlerProxy) Adapter_descriptor() (*empty.Empty, error) {
    return new(empty.Empty), nil    
}

func (rhp *RequestHandlerProxy) Device_types() (*voltha.DeviceTypes, error) {
    return nil, nil
}

func (rhp *RequestHandlerProxy) Health() (*voltha.HealthStatus, error) {    
    return nil, nil
}

下面是我在适配器文件中的接口:

type IAdapter interface {
    Adapter_descriptor() error
    Device_types() (*voltha.DeviceTypes, error)
    Health() (*voltha.HealthStatus, error)
}

【问题讨论】:

  • 请使用 go fmt 格式化您的代码,然后您想参加 Go tour tour.golang.org/welcome/1
  • 如果您需要语言基础方面的帮助,请提供一个最小示例。
  • 感谢您的回复。我正在制作 UT 案例,所以我无法使用 fmt。

标签: go methods struct interface field


【解决方案1】:

您提供的代码有点难以理解(不完整且目的不明),所以这里有一个简化的示例,希望能回答您的问题。请注意,从问题标题中我假设RequestHandlerProxy 实现接口IAdapter 的事实让您感到困惑;这可能是无关紧要的(可能有其他函数接受IAdapter,其中传入RequestHandlerProxy 或您自己的IAdapter 实现是有意义的,例如下面的adaptImpl)。

我已将IAdapter 简化为包含一个函数并将所有内容放在一个文件中。要扩展它以在您的示例中工作,您需要实现所有三个函数(Adapter_descriptor()Device_types()Health())。代码可以在go playground 中运行(如果这不能回答您的问题,也许您可​​以修改该代码以提供问题的简化示例)。

package main
import "errors"

// IAdapter - Reduced to one function to make this simple
type IAdapter interface {
    Adapter_descriptor() error
}

/// NewRequestHandlerProxy - Simplified version of the New function
func NewRequestHandlerProxy(iadapter IAdapter) {
    return // code removed to make example simple
}

// adaptImpl - Implements the IAdapter interface
type adaptImpl struct {
    isError bool // example only
}

// Adapter_descriptor - Implement the function specified in the interface
func (a adaptImpl) Adapter_descriptor() error {
    if a.IsError {
        return errors.New("An error happened")
    }
    return nil
}

func main() {
    // Create an adaptImpl which implements IAdapter 
    x := adaptImpl{isError: true}
    NewRequestHandlerProxy(x)
}

【讨论】:

  • 您定义的上述示例非常清楚并且我意识到这一点。调用方法的方法是将值传递给 struct 并使用 struct 我们可以调用您所做的方法。但就我而言,这有点困难。 Sine 我正在为无法修改上述源代码的代码制作 UT。只是想通过它。
  • 如果我对您的理解正确,您需要在不修改上述任何代码的情况下拨打NewRequestHandlerProxy 吗?如果是这样,那么我的例子就成立了;你需要传入实现IAdapter的东西;这可能是您自己创建的东西(接口的重点是您可以传入任何实现该接口的东西)或使用其他地方提供的现有结构(请注意,您不能使用 RequestHandlerProxy 本身,因为这将是递归的)。就目前情况而言,您没有提供足够的信息来提供更详细的回复。
  • 进行了快速搜索 - 您的代码来自 voltha 包(说明这一点会很有帮助)。在这种情况下,我会向您推荐该软件包中的一个示例; github.com/opencord/voltha-openolt-adapter/blob/master/… 实现了 IAdapter 接口。
  • 非常感谢。这是您提供的链接的一个很好的例子。问题是我们需要通过定义然后使用我们可以调用函数的值来在另一端创建一个虚拟结构。非常有帮助。再次感谢。
猜你喜欢
  • 2017-02-09
  • 2017-12-28
  • 1970-01-01
  • 1970-01-01
  • 2015-07-14
  • 2018-09-22
  • 2019-02-06
  • 2018-09-19
  • 1970-01-01
相关资源
最近更新 更多