Home > Backend Development > Golang > How to Register Go Packages Without Creating Cyclic Dependencies?

How to Register Go Packages Without Creating Cyclic Dependencies?

DDD
Release: 2024-12-21 06:52:10
Original
833 people have browsed it

How to Register Go Packages Without Creating Cyclic Dependencies?

Registering Packages in Go without Cyclic Dependency

Problem:

You have a central package that provides interfaces and depends on other packages that offer implementations of those interfaces. However, including these dependent packages in the central package creates a cyclic dependency, which Go does not allow.

Standard Library Solutions:

  • Without Central Registry: Define interfaces in one package and concrete implementations in separate packages. The user explicitly specifies which implementation to use.
  • With Central Registry: Implementations register themselves in a central registry (often via package init() functions). This allows for extensibility but requires manual registration.

Custom Registry Solution:

  • Create an additional package (pf) that provides "factory" methods to instantiate specific implementations.
  • The factory package depends on both the interface package (pi) and implementation packages (pa, pb, etc.), but this does not create a cycle because pf does not depend on itself.

Choosing the Best Solution:

The ideal approach depends on specific requirements:

  • If you can predetermine the implementation to use, opt for the first solution (without a central registry).
  • If extensibility is critical and you cannot predict the implementation, consider the second or third solution (with a central or custom registry).

Code Example for Custom Registry Solution:

// Package pi defines an interface I.
package pi

type I interface {
    // Some method.
    DoSomething()
}

// Package pa implements I with type A.
package pa

import "pi"

type A struct{}

func (a *A) DoSomething() {
    // Some implementation.
}

// Package pb implements I with type B.
package pb

import "pi"

type B struct{}

func (b *B) DoSomething() {
    // Some implementation.
}

// Package pf provides a factory to create instances of I.
package pf

import (
    "pi"
    "pa"
    "pb"
)

// NewClient returns an instance of I based on a flag.
func NewClient(flag string) pi.I {
    switch flag {
    case "a":
        return &pa.A{}
    case "b":
        return &pb.B{}
    default:
        panic("Invalid flag")
    }
}
Copy after login

The above is the detailed content of How to Register Go Packages Without Creating Cyclic Dependencies?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template