db.go 435 B

1234567891011121314151617181920212223
  1. package db
  2. import (
  3. "database/sql"
  4. "github.com/dmitriygnatenko/internal/interfaces"
  5. )
  6. func Init(env interfaces.IEnv) (*sql.DB, error) {
  7. dataSource := env.GetDBUser() + ":" + env.GetDBPassword() +
  8. "@tcp(" + env.GetDBHost() + ":" + env.GetDBPort() + ")/" + env.GetDBName()
  9. db, err := sql.Open("mysql", dataSource)
  10. if err != nil {
  11. return nil, err
  12. }
  13. if err = db.Ping(); err != nil {
  14. return nil, err
  15. }
  16. return db, nil
  17. }