recently I have seen that all the company"s projects are switching to dig as di,ioc. I am very repulsive, but I feel that I don"t have to be a maverick. I came to the community to ask all the bosses why so many people admire dig.
to use dig, you really only need to provide once in main.
//main.go
func buildContainer() *dig.Container {
container := dig.New()
container.Provide(models.InitStorager)
//container.Provide(api.DI_Tools)
return container
}
the point is that every time you need to use this object, there are all kinds of Invoke
.//router.go
var data *models.OperatorUser
db.Invoke(func(s *models.Storager) { //w.Header["userid"]
data, ok = s.OperatorUser.Get(&models.OperatorUserGetParam{userid, validForm.Owner})
})
var userid string
var ok bool
db.Invoke(func(s *models.Storager) {
Mlok, Mlid := s.OperatorUser.MlidIdGet(validForm.Owner)
USeridok, USerid := s.OperatorUser.UseridGet(validForm.Owner)
if !Mlok || !USeridok {
return
}
userid = USerid
ok = s.OperatorUser.Set(&models.OperatorUserSetParam{
Userid: USerid,
Mlud: Mlid,
Email: utils.emailBuild(USerid), //// oper_guid@mail.com
Avatar: validForm.Avatar,
//Phone: Input.Phone,
Nickname: validForm.Nickname,
Owner: validForm.Owner})
})
//
when you see the whole code, you can see Invoke everywhere. The whole person is not good. Not to mention the performance impact caused by di framework by wwcd Daniel, just seeing this code makes you feel gopher at all.
//new,funcinit()
db:=new(models.Storager)
db:=InitStorager("config.yml" || config)
feels better than the whole code is Invoke, at least I know what I want, nil,panic is easier to check.
I don"t know why so many people feel that using global variables and using init is abusive.
* I don"t reject dependency injection. I have to say the important things three times.
I feel that go should follow the road to simplicity. I believe that using go to write micro services, gadgets still account for the majority, and there is no need to integrate a service 2W lines of code into 3W lines of code.