Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

how to struct go code, should I inject every dependency?

When I build a golang app, especially in logic handler layer, you will have many package and struct dependencies.

For example:

import "a"
import "b"

type Handler struct {
    a a.TypeA
}

func NewHandler(a a.TypeA) *Handler{
    return &Handler{
              a: a,
    }
}

func (h *Handler) handle(req *Request) *Response {
    h.a.funcA(req.Param1)
    b.funcB(req.Param2)
    return &Response{}
} 

As you see, I can import a package function or a struct method in handle, but for decoupling and test, I inject a.TypeA dependency.

But b.funcB() is a package function, I'm confused about if I should inject b.funcB() when NewHandler(a.TypeA, func()). Should I inject every dependency?

But for golang's package, like 'os', 'math', why I don't need to inject and can use directly, so, which package or struct I could use directly? which I need inject?

Comments