Nav apraksta

database.go 549B

123456789101112131415161718192021222324252627282930313233343536
  1. package database
  2. import (
  3. "database/sql"
  4. "log"
  5. "../../config"
  6. _ "github.com/go-sql-driver/mysql"
  7. )
  8. var DB *sql.DB
  9. func Init() error {
  10. //user:pass@tcp(host:port)/schema?parseTime=true
  11. var dataSource = config.DATABASE_USER + ":" +
  12. config.DATABASE_PASS + "@tcp(" +
  13. config.DATABASE_HOST + ":" +
  14. config.DATABASE_PORT + ")/" +
  15. config.DATABASE_SCHEMA + "?parseTime=true"
  16. var err error
  17. DB, err = sql.Open("mysql", dataSource)
  18. if err != nil {
  19. log.Panic(err)
  20. }
  21. if err = DB.Ping(); err != nil {
  22. log.Panic(err)
  23. }
  24. return err
  25. }