封面
This commit is contained in:
38
configs/mysql.go
Normal file
38
configs/mysql.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package configs
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
// 數據庫配置
|
||||
type DBConfig struct {
|
||||
Type string `json:"type"`
|
||||
Host string `json:"host"`
|
||||
Port int `json:"port"`
|
||||
UserName string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
Database string `json:"database"`
|
||||
}
|
||||
|
||||
var conf DBConfig
|
||||
|
||||
func init() {
|
||||
data, err := ioutil.ReadFile("./data/config.json")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
err = json.Unmarshal(data, &conf)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
func GetMySQL() (*sql.DB, error) {
|
||||
return sql.Open(conf.Type, conf.UserName+":"+conf.Password+"@/"+conf.Database)
|
||||
}
|
Reference in New Issue
Block a user