package db import ( "fmt" _ "github.com/go-sql-driver/mysql" "github.com/jmoiron/sqlx" ) type Config struct { UserName string Password string Ip string Port int DbName string Charset string } // 连接数据库 func ConnectMysql(conf *Config) *sqlx.DB { dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s", conf.UserName, conf.Password, conf.Ip, conf.Port, conf.DbName, conf.Charset) Db, err := sqlx.Open("mysql", dsn) if err != nil { fmt.Printf("mysql connect failed, detail is [%v]", err.Error()) } return Db }