Nenhuma descrição

database.go 792B

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