Nenhuma descrição

database.go 702B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package database
  2. import (
  3. "database/sql"
  4. "log"
  5. _ "github.com/go-sql-driver/mysql"
  6. )
  7. //DB : Conexão com db
  8. var DB *sql.DB
  9. //Config : struct do banco de dados
  10. type Config struct {
  11. Host string
  12. Port string
  13. User string
  14. Pass string
  15. Schema string
  16. }
  17. //Init : iniciação do metodo de conexão
  18. func Init(config Config) error {
  19. //user:pass@tcp(host:port)/schema?parseTime=true
  20. var dataSource = config.User + ":" +
  21. config.Pass + "@tcp(" +
  22. config.Host + ":" +
  23. config.Port + ")/" +
  24. config.Schema + "?parseTime=true"
  25. var err error
  26. DB, err = sql.Open("mysql", dataSource)
  27. if err != nil {
  28. log.Panic(err)
  29. }
  30. if err = DB.Ping(); err != nil {
  31. log.Panic(err)
  32. }
  33. return err
  34. }