最佳模式是在不引发循环依赖的情况下重用Go接口。

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

What is best pattern to reuse Go interface without tripping cyclic dependencies

问题

我有一个简单的包声明,其中包"a"定义了一个接口"A",但我需要在包"b"中使用该接口进行类型推断,并在"a"的"DoRequest()"中实现b.Request()的实现,这意味着必须以循环方式导入包。

我的问题是,是否有一种简单的方法来避免编译器循环依赖错误的设计?

注意:为了避免将"a"和"b"放在同一个包中。

包"b"的声明如下:

package b

import "a"

func Request(t a.A) {
    m := t.GetMethod()
    payload := t.GetPayload()
}

包"a"的声明如下:

package a

import "b"

type A interface {
    GetMethod() string
    GetPayload() string
}

type ImplementA struct {}

func (imp ImplementA) GetMethod() string {
    return ""
}

func (imp ImplementA) GetPayload() string {
    return ""
}

func (imp ImplementA) DoRequest() {
    b.Request(imp)
}

请注意,以上是要翻译的内容。

英文:

I have this simple package declaration where package "a" defines an interface "A" but I need to use the interface in package "b" for type inference and then the implementation of b.Request() in DoRequest() of "a" this means having to import the package in a cyclic way.

My question is if there is a none complicated approach to this design to avoid compiler cyclic dependency error ?.

NOTE to avoid putting "a" and "b" in the same package

package b

import "a"

func Request(t a.A){
m := t.GetMethod()
payload := t.GetPayload()
}

And Package "a" declaration

package a

import "b"


type A interface {
 GetMethod () string
 GetPayload () string
}

type ImplementA struct {
}

func (imp ImplementA)GetMethod() string{
return ""
}

func (imp  ImplementA) GetPayload() string{
return ""
}

func (imp ImplementA) DoRequest(){
  b.Request(imp)
}

答案1

得分: 4

在Go语言中,通常最佳实践是在使用它们的地方定义接口。因此,在package b中,可以定义一个包含package b中函数所需方法的接口。

你仍然可以在package a中添加其他函数。如果你还需要在package a中定义一个接口,可以将package b中的接口嵌入其中。

例如:

package b

type AInterface interface {
  GetMethod () string
  GetPayload () string
}

func Request(t AInterface) {
  m := t.GetMethod()
  payload := t.GetPayload()
}

然后,package a只需包含实现。

package a

import "b"

type ImplementA struct {
}

func (imp ImplementA) GetMethod() string {
  return ""
}

func (imp ImplementA) GetPayload() string {
  return ""
}

func (imp ImplementA) DoRequest() {
  b.Request(imp)
}
英文:

It is considered best practice in go to define interfaces where they are used. So in package b, define an interface with those methods required by the function in package b.

You can still add other functions in package a. The interface from package b can be embedded if you also need to define an interface in package a.

For example:

package b

type AInterface interface {
  GetMethod () string
  GetPayload () string
}

func Request(t AInterface) {
  m := t.GetMethod()
  payload := t.GetPayload()
}

Then package A would just contain the implementation.

package a

import "b"

type ImplementA struct {
}

func (imp ImplementA) GetMethod() string {
  return ""
}

func (imp ImplementA) GetPayload() string {
  return ""
}

func (imp ImplementA) DoRequest() {
  b.Request(imp)
}

huangapple
  • 本文由 发表于 2023年2月5日 12:08:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75350059.html
匿名

发表评论

匿名网友

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

确定