duandong9195 2016-01-23 17:02
浏览 10
已采纳

IOC,接口和实现的工作方式

I am trying to figure out interfaces in GoLang.

Being familiar with how it all works in Java i.e. interface, implements allowing for a mock and a full implementation. I am a bit confused on how I can tie things together in Go.

For example, I am trying to implement AmazonProductAdvertisingAPI connector. I have created PAAPI interface and also another file that provides an implementation.

I then have a struct config. I then created a file with method signatures that implement PAAPI and are based on config i.e. config implements PAAPI.

All that happens in the same package. But from an outside perspective, how can I then go and implement a mock of PAAPI it seems bizarre that everything is linked on the config struct.

Any code example would be greatly appreciated. Thanks.

  • 写回答

1条回答 默认 最新

  • drtoaamk20278 2016-01-24 00:28
    关注

    If I am understanding you correctly, you're saying your implementation is coupled to the config struct instead of the interface. If that is the case, simply replace the argument that uses the config to the interface, in this case PAAPI.

    In the below code, instead of the DoSomethingWithStruct function definition, what you want is probably the DoSomethingWithInterface function definition.

    type PAAPI interface {
        Foo() // just a stub method
    }
    
    type Config struct {}
    
    func (config *Config) Foo() {
        // do something
    }
    
    func DoSomethingWithStruct(config Config) {
        // do something
    }
    
    // you probably want a method that uses your config through the interface
    func DoSomethingWithInterface(config PAAPI) {
        // do something
    }
    

    To take it another step further, if in an external package, you want to implement the interface simply define another struct that adheres to the interface. Like the below:

    type ExternalConfig struct{}
    
    func (config *ExternalConfig) Foo() {
        // do something
    }
    

    A couple notes about this that differs from Java is that there is no explicit implements keyword in Go. It is based off a familiar principle in dynamically typed programming languages instead, which is basically duck-typing. It implicitly knows that the interface is implemented by the struct at compile time.

    Based on the two previous snippets of code, now you can call DoSomethingWithInterface(ExternalConfig{}). Note that I inlined the instance of ExternalConfig here but you can create it with actual credentials and pass it into DoSomethingWithInterface in the same way. Hope this helps. Also, feel free to comment to ask for clarity if I'm not quite hitting the target.

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

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大