Answer the question
In order to leave comments, you need to log in
How to pass database connection information to golang package?
How, from the point of view of the architecture of the application on GO, to pass a database connection to all packages where it will be used?
Let's say now I do in the main package:
db, err := sql.Open("mysql", "database:[email protected](127.0.0.1:3306)/database?parseTime=true")
servers.SocketsRun(db)
connect.run(db)
Answer the question
In order to leave comments, you need to log in
Would this option be correct?
https://fooobar.com/questions/10380979/sharing-ag...
You can use Dependency Injection by passing the database connection to the struct instantiation function
type Server struct {
db *sql.DB
}
func NewServer(db *sql.DB) *Server {
return &Server{
db: db,
}
}
func (s *Server) Run() {
// s.db
}
Use DI and IoC
Run-time: https://github.com/uber-go/dig
Compile-time: https://github.com/google/wire
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question