34 lines
578 B
Go
34 lines
578 B
Go
package db
|
|
|
|
import (
|
|
"database/sql"
|
|
"user/config"
|
|
"fmt"
|
|
_ "github.com/go-sql-driver/mysql"
|
|
"log"
|
|
)
|
|
|
|
var gDb Database
|
|
|
|
func Init() {
|
|
mysqlconf := config.GetMysqlConfig()
|
|
log.Println(mysqlconf)
|
|
cnn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8",mysqlconf.UserName,mysqlconf.Password,
|
|
mysqlconf.Addr,mysqlconf.Db)
|
|
_db,err := sql.Open("mysql",cnn)
|
|
if err != nil{
|
|
fmt.Println("connect sql server ",err.Error())
|
|
}
|
|
e := _db.Ping()
|
|
if nil != e{
|
|
fmt.Println(e.Error())
|
|
}
|
|
gDb = Database{Type:string(""),DB:_db}
|
|
}
|
|
|
|
func GetMysqlClient() *Database {
|
|
return &gDb
|
|
}
|
|
|
|
|