is it possible to make simple dependency injection in golang

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

is it possible to make simple dependency injection in golang

问题

我来自Node.js和Nest.js的世界,那里的依赖注入(DI)非常简单,并由框架提供。在使用Go构建服务时,我是否可以或应该考虑使用依赖注入?

英文:

I come from node.js and nest.js world, where DI is simple and provided by a framework. Can or should I consider using dependency injection when building services on go?

答案1

得分: 3

是的,Go语言中是可以实现依赖注入的。

以下是一个简单的三步骤的Go语言依赖注入系统:

假设你有一个包A,它不应该导入包B,但需要调用该包中的一些函数,例如Load()和Save()。

  1. 在包A中,定义一个包含这些函数的接口类型。
type Storage interface {
	Load(string) []byte
	Save(string, []byte)
}

包A中的类型可以引用该接口,并调用Load()和Save(),而不需要知道这些调用的实际接收者。

type StructA struct {
	content []byte
	storage Storage
}

func NewStructA(s Storage) *StructA {
	return &StructA{
		content: ...,
		storage: s,
	}
}

func (a *StructA) Save(name string) {
	a.storage.Save(name, a.content)
}

func (a *StructA) Load(name string) {
	a.content = a.storage.Load(name)
}
  1. 在包B中,实现Load()和Save()。
type StoreB struct {
	data map[string][]byte
}

func (b *StoreB) Save(name string, contents []byte) {
	b.data[name] = contents
}

func (b *StoreB) Load(name string) []byte {
	return b.data[name]
}
  1. main包中,连接各个组件。
storage := &B.StoreB{}
a := A.NewStructA(storage)
a.Save()

现在你可以添加其他存储提供者(包C、D等),并在main中将它们连接起来。

storage2 := &C.StoreC{}
a2 := A.NewStructA(storage2)
a2.Save()

更详细的讨论可以参考这里:https://appliedgo.net/di/

英文:

Yes, it is possible in Go.

A simple three-step DI system for Go:

Imagine you have a package A that shall not import package B but call some functions from that package; for example, functions Load() and Save().

  1. In package A, define an interface type with these functions.
type Storage interface {
	Load(string) []byte
	Save(string, []byte)
}

A type in package A can then refer to that interface and call Load() and Save() without knowing the actual receivers of these calls.

type StructA struct {
	content []byte
	storage Storage
}

func NewStructA(s Storage) *StructA {
	return &StructA{
		content: ...,
		storage: s,
	}
}

func (a *StructA) Save(name string) {
	a.storage.Save(name, a.content)
}

func (a *StructA) Load(name string) {
	a.content = a.storage.Load(name)
}
  1. In package B, implement Load() and Save().
type StoreB struct {
	poem []byte
}

func (b *StoreB) Save(name string, contents []byte) {
    // let's say StoreB contains a map called data
	b.data[name] = contents
}

func (b *StoreB) Load(name string) []byte {
	return b.data[name]
}
  1. In package main, connect the wires.
storage := B.StructB
a := A.NewStructA(storage)
a.Save()

Now you can add other storage provides (package C, D,...) and wire them up in main.

storage2 := C.StructC
a2 := A.NewStructA(storage2)
a2.Save()

A more detailed discussion is here: https://appliedgo.net/di/

答案2

得分: 2

一个基于依赖注入的Go应用程序框架。
https://github.com/uber-go/fx

一个基于反射的Go依赖注入工具包。https://github.com/uber-go/dig

英文:

A dependency injection based application framework for Go.
https://github.com/uber-go/fx

A reflection based dependency injection toolkit for Go. https://github.com/uber-go/dig

答案3

得分: 0

是的,你应该在Go语言中考虑使用依赖注入(DI),DI在Go语言中具有与其他语言相同的优势。通过使用接口,可以很容易地在Go语言中实现依赖注入。

英文:

Yes, you should consider DI in go, DI has same advantages in go as any other language. It can be easily achieved in go by using interfaces.

huangapple
  • 本文由 发表于 2022年9月3日 22:35:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/73593034.html
匿名

发表评论

匿名网友

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

确定