'How to solve circular dependency without creating new package?

In golang want a plugins system with a global registry. Structure can be simplified as follows:

/plugins/registry.go
/plugins/plugin1/impl.go

registry.go:

package plugins

import "plugins/plugin1" // required for plugin1.MakePlugin

type IPlugin interface {
  Register() error
}

type Factory func(x int) IPlugin

var registry = []Factory{
  plugin1.MakePlugin,
}

impl.go:

package plugin1

import "plugins" // required for IPlugin

type Plugin struct {
  x int
}

func MakePlugin(x int) plugins.IPlugin {
  return &Plugin{
    x: x,
  }
}

How to solve without moving IPlugin to 3rd (e.g common) package?

PS. I thought if i return *Plugin from MakePlugin this will be compatible with Factory, but its not!!!



Solution 1:[1]

Try to separate the Interfaces from their implementation to external packages that are abstract. Then, depend on them according to the Dependency injection and Adapter pattern paradigms.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tamir