db.go 351 B

123456789101112131415161718192021
  1. package util
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "sync-block/er"
  6. _ "github.com/go-sql-driver/mysql"
  7. )
  8. func ConnectDb(host, id, pw, dbName string) *sql.DB {
  9. db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?parseTime=true",
  10. id, pw, host, dbName))
  11. er.Check(err)
  12. er.Check(db.Ping())
  13. return db
  14. }
  15. func NewNoopDB() DB {
  16. return &NoopDB{}
  17. }