Is it really better for Golang to use dig than direct func injection and new?

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.


I don't evaluate whether the code is good or not (although I want to complain), but I feel really uncomfortable. I'm tired to read the code. If I can do it step by step, I have to take several steps. I feel good when I write it, regardless of how others feel when I maintain it.

some global variables are reasonable, such as db,config.
init is also normal, but the order of execution should be sorted out.


in fact, the main reason is that his writing is too ugly.
see if this looks better:

func BuildContainer() *dig.Container {
    container := dig.New()
    
    container.Provide(NewConfig)
    container.Provide(NewPersonService)
    container.Provide(NewServer)
    container.Provide(NewDB)
    container.Provide(ConnectDatabase)
    container.Provide(NewPersonRepository)

    return container
}

var myDB *DB

func main() {
    container := BuildContainer()
   
    err := container.Invoke(func(db *Server) {
        myDB = db
    })

    err := container.Invoke(func(server *Server) {
        server.Run()
    })

    if err != nil {
        panic(err)
    }
}

dig should not be abused. I personally feel that it should be used only during initialization. I can see that I register Provide out of order in the code, and then I don't need to consider the order of execution when I Invoke in main.
1.dig uses reflection, and this performance-affecting operation should not be commonly used
2. It should be used for initialization, such as service and db, and assign a value to a global variable

after initialization.

go's dependency injection hasn't been used much yet, but it seems that all of them are bullish on wire . It is also used in go-cloud .


use this simple and rude https://github.com/bzeron/inject


maintenance, really do not understand. Try not to be complicated as simple as possible


isn't Invoke used for initialization?
what is said in the document:

container.Invoke(function(s *Server) {
    s.StartXXX()
})
Menu