How ioc, interfaces and implementations work

huangapple go评论82阅读模式
英文:

How ioc, interfaces and implementations work

问题

我正在尝试理解Go语言中的接口。

在Java中,我熟悉接口和实现的工作原理,可以使用接口和实现来进行模拟和完整实现。但是在Go语言中,我对如何将这些东西联系在一起感到有些困惑。

例如,我正在尝试实现AmazonProductAdvertisingAPI连接器。我已经创建了PAAPI接口,并在另一个文件中提供了一个实现。

然后,我有一个名为config的结构体。我创建了一个包含实现了PAAPI接口的方法签名的文件,这些方法基于config结构体,也就是说,config实现了PAAPI接口。

所有这些都发生在同一个包中。但是从外部的角度来看,我该如何实现PAAPI的模拟呢?似乎一切都与config结构体相关联,这有些奇怪。

如果能提供一些代码示例,我将不胜感激。谢谢。

英文:

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

得分: 1

如果我理解正确,你的意思是你的实现与config结构耦合,而不是接口。如果是这样的话,只需将使用config的参数替换为接口,即PAAPI

在下面的代码中,你可能想要的是DoSomethingWithInterface函数定义,而不是DoSomethingWithStruct函数定义。

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
}

进一步说,如果在外部包中,你想要实现该接口,只需定义另一个符合接口的结构体,如下所示:

type ExternalConfig struct{}

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

关于这一点,与Java不同的是,Go语言中没有显式的implements关键字。它基于动态类型编程语言中的一个熟悉原则,即鸭子类型。它在编译时隐式地知道该结构体实现了接口。

基于前面两段代码,现在你可以调用DoSomethingWithInterface(ExternalConfig{})。请注意,我在这里内联了ExternalConfig的实例,但你可以使用实际的凭据创建它,并以相同的方式将其传递给DoSomethingWithInterface。希望这可以帮到你。如果我没有完全理解你的问题,请随时评论以便我更好地回答。

英文:

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.

huangapple
  • 本文由 发表于 2016年1月24日 01:02:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/34966271.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定