I've implemented dao.go
file with the following realization:
type DbClient struct { db *gorm.DB }
GetDBClient()
initializes connection with database and returns(*DbClient, error)
func (db *DbClient) Close() { db.db.Close() }
- Different CRUD methods of
DbClient
And the main.go
file that serves all the handlers consumes it like this:
var dbClient *DbClient
func main() {
db, err := GetDBClient()
if err != nil {
panic(err)
}
dbClient = db
defer dbClient.Close()
...
}
So all the handlers of the main.go
use global dbClient
.
Is this architecture thread safe and does it provide atomicity of operations with database?