29 lines
539 B
Go
29 lines
539 B
Go
package gorm
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
//Driver to MariaDB
|
|
_ "github.com/jinzhu/gorm/dialects/mysql"
|
|
)
|
|
|
|
//NewMySQLConnection returns a connection to a database
|
|
func NewMySQLConnection(host, port, name, user, pass string) (*gorm.DB, error) {
|
|
dbConfig := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&parseTime=True&loc=Local",
|
|
user,
|
|
pass,
|
|
host,
|
|
port,
|
|
name,
|
|
)
|
|
|
|
db, err := gorm.Open("mysql", dbConfig)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to connect to database: %v", err)
|
|
}
|
|
|
|
return db, nil
|
|
}
|