Go 依赖注入库dig

2024-09-08 13:38
文章标签 go 依赖 注入 dig

本文主要是介绍Go 依赖注入库dig,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

简介

今天我们来介绍 Go 语言的一个依赖注入(DI)库——dig。dig 是 uber 开源的库。Java 依赖注入的库有很多,相信即使不是做 Java 开发的童鞋也听过大名鼎鼎的 Spring。相比庞大的 Spring,dig 很小巧,实现和使用都比较简洁。

快速使用

第三方库需要先安装,由于我们的示例中使用了前面介绍的go-ini和go-flags,这两个库也需要安装:

$ go get go.uber.org/dig
$ go get gopkg.in/ini.v1
$ go get github.com/jessevdk/go-flags

下面看看如何使用:

package mainimport ("fmt""github.com/jessevdk/go-flags""go.uber.org/dig""gopkg.in/ini.v1"
)type Option struct {ConfigFile string `short:"c" long:"config" description:"Name of config file."`
}func InitOption() (*Option, error) {var opt Option_, err := flags.Parse(&opt)return &opt, err
}func InitConf(opt *Option) (*ini.File, error) {cfg, err := ini.Load(opt.ConfigFile)return cfg, err
}func PrintInfo(cfg *ini.File) {fmt.Println("App Name:", cfg.Section("").Key("app_name").String())fmt.Println("Log Level:", cfg.Section("").Key("log_level").String())
}func main() {container := dig.New()container.Provide(InitOption)container.Provide(InitConf)container.Invoke(PrintInfo)
}

在同一目录下创建配置文件my.ini

app_name = awesome web
log_level = DEBUG[mysql]
ip = 127.0.0.1
port = 3306
user = dj
password = 123456
database = awesome[redis]
ip = 127.0.0.1
port = 6381

运行程序,输出:

$ go run main.go -c=my.ini
App Name: awesome web
Log Level: DEBUG

dig库帮助开发者管理这些对象的创建和维护,每种类型的对象会创建且只创建一次。
dig库使用的一般流程:

  • 创建一个容器:dig.New;
  • 为想要让dig容器管理的类型创建构造函数,构造函数可以返回多个值,这些值都会被容器管理;
  • 使用这些类型的时候直接编写一个函数,将这些类型作为参数,然后使用container.Invoke执行我们编写的函数。

参数对象

有时候,创建对象有很多依赖,或者编写函数时有多个参数依赖。如果将这些依赖都作为参数传入,那么代码将变得非常难以阅读:

container.Provide(func (arg1 *Arg1, arg2 *Arg2, arg3 *Arg3, ....) {// ...
})

dig支持将所有参数打包进一个对象中,唯一需要的就是将dig.In内嵌到该类型中:

type Params {dig.InArg1 *Arg1Arg2 *Arg2Arg3 *Arg3Arg4 *Arg4
}container.Provide(func (params Params) *Object {// ...
})

内嵌了dig.In之后,dig会将该类型中的其它字段看成Object的依赖,创建Object类型的对象时,会先将依赖的Arg1/Arg2/Arg3/Arg4创建好。

package mainimport ("fmt""log""github.com/jessevdk/go-flags""go.uber.org/dig""gopkg.in/ini.v1"
)type Option struct {ConfigFile string `short:"c" long:"config" description:"Name of config file."`
}type RedisConfig struct {IP   stringPort intDB   int
}type MySQLConfig struct {IP       stringPort     intUser     stringPassword stringDatabase string
}type Config struct {dig.InRedis *RedisConfigMySQL *MySQLConfig
}func InitOption() (*Option, error) {var opt Option_, err := flags.Parse(&opt)return &opt, err
}func InitConfig(opt *Option) (*ini.File, error) {cfg, err := ini.Load(opt.ConfigFile)return cfg, err
}func InitRedisConfig(cfg *ini.File) (*RedisConfig, error) {port, err := cfg.Section("redis").Key("port").Int()if err != nil {log.Fatal(err)return nil, err}db, err := cfg.Section("redis").Key("db").Int()if err != nil {log.Fatal(err)return nil, err}return &RedisConfig{IP:   cfg.Section("redis").Key("ip").String(),Port: port,DB:   db,}, nil
}func InitMySQLConfig(cfg *ini.File) (*MySQLConfig, error) {port, err := cfg.Section("mysql").Key("port").Int()if err != nil {return nil, err}return &MySQLConfig{IP:       cfg.Section("mysql").Key("ip").String(),Port:     port,User:     cfg.Section("mysql").Key("user").String(),Password: cfg.Section("mysql").Key("password").String(),Database: cfg.Section("mysql").Key("database").String(),}, nil
}func PrintInfo(config Config) {fmt.Println("=========== redis section ===========")fmt.Println("redis ip:", config.Redis.IP)fmt.Println("redis port:", config.Redis.Port)fmt.Println("redis db:", config.Redis.DB)fmt.Println("=========== mysql section ===========")fmt.Println("mysql ip:", config.MySQL.IP)fmt.Println("mysql port:", config.MySQL.Port)fmt.Println("mysql user:", config.MySQL.User)fmt.Println("mysql password:", config.MySQL.Password)fmt.Println("mysql db:", config.MySQL.Database)
}func main() {container := dig.New()container.Provide(InitOption)container.Provide(InitConfig)container.Provide(InitRedisConfig)container.Provide(InitMySQLConfig)err := container.Invoke(PrintInfo)if err != nil {log.Fatal(err)}
}

上面代码中,类型Config内嵌了dig.In,PrintInfo接受一个Config类型的参数。调用Invoke时,dig自动调用InitRedisConfigInitMySQLConfig,并将生成的*RedisConfig*MySQLConfig“打包”成一个Config对象传给PrintInfo。

运行结果:

$ go run main.go -c=my.ini
=========== redis section ===========
redis ip: 127.0.0.1
redis port: 6381
redis db: 1
=========== mysql section ===========
mysql ip: 127.0.0.1
mysql port: 3306
mysql user: dj
mysql password: 123456
mysql db: awesome

结果对象

前面说过,如果构造函数返回多个值,这些不同类型的值都会存储到dig容器中。参数过多会影响代码的可读性和可维护性,返回值过多同样也是如此。为此,dig提供了返回值对象,返回一个包含多个类型对象的对象。返回的类型,必须内嵌dig.Out

type Results struct {dig.OutResult1 *Result1Result2 *Result2Result3 *Result3Result4 *Result4
}
dig.Provide(func () (Results, error) {// ...
})

我们把上面的例子稍作修改。将Config内嵌的dig.In变为dig.Out

type Config struct {dig.OutRedis *RedisConfigMySQL *MySQLConfig
}

提供构造函数InitRedisAndMySQLConfig同时创建RedisConfigMySQLConfig,通过Config返回。这样就不需要将InitRedisConfigInitMySQLConfig加入dig容器了:

func InitRedisAndMySQLConfig(cfg *ini.File) (Config, error) {var config Configredis, err := InitRedisConfig(cfg)if err != nil {return config, err}mysql, err := InitMySQLConfig(cfg)if err != nil {return config, err}config.Redis = redisconfig.MySQL = mysqlreturn config, nil
}func main() {container := dig.New()container.Provide(InitOption)container.Provide(InitConfig)container.Provide(InitRedisAndMySQLConfig)err := container.Invoke(PrintInfo)if err != nil {log.Fatal(err)}
}

PrintInfo直接依赖RedisConfigMySQLConfig

func PrintInfo(redis *RedisConfig, mysql *MySQLConfig) {fmt.Println("=========== redis section ===========")fmt.Println("redis ip:", redis.IP)fmt.Println("redis port:", redis.Port)fmt.Println("redis db:", redis.DB)fmt.Println("=========== mysql section ===========")fmt.Println("mysql ip:", mysql.IP)fmt.Println("mysql port:", mysql.Port)fmt.Println("mysql user:", mysql.User)fmt.Println("mysql password:", mysql.Password)fmt.Println("mysql db:", mysql.Database)
}

可以看到InitRedisAndMySQLConfig返回Config类型的对象,该类型中的RedisConfigMySQLConfig都被添加到了容器中,PrintInfo函数可直接使用。

运行结果与之前的例子完全一样。

可选依赖

默认情况下,容器如果找不到对应的依赖,那么相应的对象无法创建成功,调用Invoke时也会返回错误。有些依赖不是必须的,dig也提供了一种方式将依赖设置为可选的:

type Config struct {dig.InRedis *RedisConfig `optional:"true"`MySQL *MySQLConfig
}

通过在字段后添加结构标签optional:"true",我们将RedisConfig这个依赖设置为可选的,容器中RedisConfig对象也不要紧,这时传入的Config中redis为 nil,方法可以正常调用。显然可选依赖只能在参数对象中使用。

我们直接注释掉InitRedisConfig,然后运行程序:

// 省略部分代码
func PrintInfo(config Config) {if config.Redis == nil {fmt.Println("no redis config")}
}func main() {container := dig.New()container.Provide(InitOption)container.Provide(InitConfig)container.Provide(InitMySQLConfig)container.Invoke(PrintInfo)
}

输出:

$ go run main.go -c=my.ini
no redis config

注意,创建失败和没有提供构造函数是两个概念。如果InitRedisConfig调用失败了,使用Invoke执行PrintInfo还是会报错的。

命名

前面我们说过,dig默认只会为每种类型创建一个对象。如果要创建某个类型的多个对象怎么办呢?可以为对象命名!

调用容器的Provide方法时,可以为构造函数的返回对象命名,这样同一个类型就可以有多个对象了。

type User struct {Name stringAge  int
}func NewUser(name string, age int) func() *User{} {return func() *User {return &User{name, age}}
}
container.Provide(NewUser("dj", 18), dig.Name("dj"))
container.Provide(NewUser("dj2", 18), dig.Name("dj2"))

也可以在结果对象中通过结构标签指定:

type UserResults struct {dig.OutUser1 *User `name:"dj"`User2 *User `name:"dj2"`
}

然后在参数对象中通过名字指定使用哪个对象:

type UserParams struct {dig.InUser1 *User `name:"dj"`User2 *User `name:"dj2"`
}

完整代码:

package mainimport ("fmt""go.uber.org/dig"
)type User struct {Name stringAge  int
}func NewUser(name string, age int) func() *User {return func() *User {return &User{name, age}}
}type UserParams struct {dig.InUser1 *User `name:"dj"`User2 *User `name:"dj2"`
}func PrintInfo(params UserParams) error {fmt.Println("User 1 ===========")fmt.Println("Name:", params.User1.Name)fmt.Println("Age:", params.User1.Age)fmt.Println("User 2 ===========")fmt.Println("Name:", params.User2.Name)fmt.Println("Age:", params.User2.Age)return nil
}func main() {container := dig.New()container.Provide(NewUser("dj", 18), dig.Name("dj"))container.Provide(NewUser("dj2", 18), dig.Name("dj2"))container.Invoke(PrintInfo)
}

程序运行结果:

$ go run main.go
User 1 ===========
Name: dj
Age: 18
User 2 ===========
Name: dj2
Age: 18

需要注意的时候,NewUser返回的是一个函数,由dig在需要的时候调用。

组可以将相同类型的对象放到一个切片中,可以直接使用这个切片。组的定义与上面名字定义类似。可以通过为Provide提供额外的参数:

container.Provide(NewUser("dj", 18), dig.Group("user"))
container.Provide(NewUser("dj2", 18), dig.Group("user"))

也可以在结果对象中添加结构标签group:"user"
然后我们定义一个参数对象,通过指定同样的结构标签来使用这个切片:

type UserParams struct {dig.InUsers []User `group:"user"`
}func Info(params UserParams) error {for _, u := range params.Users {fmt.Println(u.Name, u.Age)}return nil
}container.Invoke(Info)

最后我们通过一个完整的例子演示组的使用,我们将创建一个 HTTP 服务器:

package mainimport ("fmt""net/http""go.uber.org/dig"
)type Handler struct {Greeting stringPath     string
}func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {fmt.Fprintf(w, "%s from %s", h.Greeting, h.Path)
}func NewHello1Handler() HandlerResult {return HandlerResult{Handler: Handler{Path:     "/hello1",Greeting: "welcome",},}
}func NewHello2Handler() HandlerResult {return HandlerResult{Handler: Handler{Path:     "/hello2",Greeting: " ",},}
}type HandlerResult struct {dig.OutHandler Handler `group:"server"`
}type HandlerParams struct {dig.InHandlers []Handler `group:"server"`
}func RunServer(params HandlerParams) error {mux := http.NewServeMux()for _, h := range params.Handlers {mux.Handle(h.Path, h)}server := &http.Server{Addr:    ":8080",Handler: mux,}if err := server.ListenAndServe(); err != nil {return err}return nil
}func main() {container := dig.New()container.Provide(NewHello1Handler)container.Provide(NewHello2Handler)container.Invoke(RunServer)
}

我们创建了两个处理器,添加到server组中,在RunServer函数中创建 HTTP 服务器,将这些处理器注册到服务器中。
运行程序,在浏览器中输入localhost:8080/hello1localhost:8080/hello2看看。

常见错误

使用dig过程中会遇到一些错误,我们来看看常见的错误。

Invoke方法在以下几种情况下会返回一个error

无法找到依赖,或依赖创建失败;

  • Invoke执行的函数返回error,该错误也会被传给调用者。
  • 这两种情况,我们都可以判断Invoke的返回值来查找原因。

总结

本文介绍了dig库,它适用于解决循环依赖的对象创建问题。同时也有利于将关注点分离,我们不需要将各种对象传来传去,只需要将构造函数交给dig容器,然后通过Invoke直接使用依赖即可,连判空逻辑都可以省略了!

这篇关于Go 依赖注入库dig的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1148259

相关文章

每天认识几个maven依赖(ActiveMQ+activemq-jaxb+activesoap+activespace+adarwin)

八、ActiveMQ 1、是什么? ActiveMQ 是一个开源的消息中间件(Message Broker),由 Apache 软件基金会开发和维护。它实现了 Java 消息服务(Java Message Service, JMS)规范,并支持多种消息传递协议,包括 AMQP、MQTT 和 OpenWire 等。 2、有什么用? 可靠性:ActiveMQ 提供了消息持久性和事务支持,确保消

pip-tools:打造可重复、可控的 Python 开发环境,解决依赖关系,让代码更稳定

在 Python 开发中,管理依赖关系是一项繁琐且容易出错的任务。手动更新依赖版本、处理冲突、确保一致性等等,都可能让开发者感到头疼。而 pip-tools 为开发者提供了一套稳定可靠的解决方案。 什么是 pip-tools? pip-tools 是一组命令行工具,旨在简化 Python 依赖关系的管理,确保项目环境的稳定性和可重复性。它主要包含两个核心工具:pip-compile 和 pip

Go Playground 在线编程环境

For all examples in this and the next chapter, we will use Go Playground. Go Playground represents a web service that can run programs written in Go. It can be opened in a web browser using the follow

go基础知识归纳总结

无缓冲的 channel 和有缓冲的 channel 的区别? 在 Go 语言中,channel 是用来在 goroutines 之间传递数据的主要机制。它们有两种类型:无缓冲的 channel 和有缓冲的 channel。 无缓冲的 channel 行为:无缓冲的 channel 是一种同步的通信方式,发送和接收必须同时发生。如果一个 goroutine 试图通过无缓冲 channel

深入理解数据库的 4NF:多值依赖与消除数据异常

在数据库设计中, "范式" 是一个常常被提到的重要概念。许多初学者在学习数据库设计时,经常听到第一范式(1NF)、第二范式(2NF)、第三范式(3NF)以及 BCNF(Boyce-Codd范式)。这些范式都旨在通过消除数据冗余和异常来优化数据库结构。然而,当我们谈到 4NF(第四范式)时,事情变得更加复杂。本文将带你深入了解 多值依赖 和 4NF,帮助你在数据库设计中消除更高级别的异常。 什么是

如何确定 Go 语言中 HTTP 连接池的最佳参数?

确定 Go 语言中 HTTP 连接池的最佳参数可以通过以下几种方式: 一、分析应用场景和需求 并发请求量: 确定应用程序在特定时间段内可能同时发起的 HTTP 请求数量。如果并发请求量很高,需要设置较大的连接池参数以满足需求。例如,对于一个高并发的 Web 服务,可能同时有数百个请求在处理,此时需要较大的连接池大小。可以通过压力测试工具模拟高并发场景,观察系统在不同并发请求下的性能表现,从而

【Go】go连接clickhouse使用TCP协议

离开你是傻是对是错 是看破是软弱 这结果是爱是恨或者是什么 如果是种解脱 怎么会还有眷恋在我心窝 那么爱你为什么                      🎵 黄品源/莫文蔚《那么爱你为什么》 package mainimport ("context""fmt""log""time""github.com/ClickHouse/clickhouse-go/v2")func main(

PHP防止SQL注入详解及防范

SQL 注入是PHP应用中最常见的漏洞之一。事实上令人惊奇的是,开发者要同时犯两个错误才会引发一个SQL注入漏洞。 一个是没有对输入的数据进行过滤(过滤输入),还有一个是没有对发送到数据库的数据进行转义(转义输出)。这两个重要的步骤缺一不可,需要同时加以特别关注以减少程序错误。 对于攻击者来说,进行SQL注入攻击需要思考和试验,对数据库方案进行有根有据的推理非常有必要(当然假设攻击者看不到你的

PHP防止SQL注入的方法(2)

如果用户输入的是直接插入到一个SQL语句中的查询,应用程序会很容易受到SQL注入,例如下面的例子: $unsafe_variable = $_POST['user_input'];mysql_query("INSERT INTO table (column) VALUES ('" . $unsafe_variable . "')"); 这是因为用户可以输入类似VALUE”); DROP TA

PHP防止SQL注入的方法(1)

(1)mysql_real_escape_string – 转义 SQL 语句中使用的字符串中的特殊字符,并考虑到连接的当前字符集 使用方法如下: $sql = "select count(*) as ctr from users where username ='".mysql_real_escape_string($username)."' and password='". mysql_r