Skip to content

Instantly share code, notes, and snippets.

@NotBadPad
Created July 24, 2014 02:19
Show Gist options
  • Save NotBadPad/ca7e8a677e40bc546f62 to your computer and use it in GitHub Desktop.
Save NotBadPad/ca7e8a677e40bc546f62 to your computer and use it in GitHub Desktop.
go:mysql-conf
package utils
import (
"database/sql"
_ "github.com/go-sql-driver/mysql"
"strconv"
)
/**
* conf/app.conf
* mysqlDSN = uname:upwd@tcp(ip:3306)/db-name?autocommit=true&charset=utf8
* mysql.maxIdleConns = 10
* mysql.maxActives = 200
*/
/**
* main.go
* DB, err := utils.OpenMysql()
* if err != nil {
* utils.Logger.Error("init mysql-db dw connection failure:", err.Error())
* return
* } else {
* utils.Logger.Info("init mysql-db dw connection successful.")
* defer DB.Close()
* }
* utils.DB = DB
*/
var DB *sql.DB
func OpenMysql() (db *sql.DB, err error) {
db, err = sql.Open("mysql", Config["mysql"])
if db != nil {
maxIdleConns, _ := strconv.ParseInt(Config["mysql.maxIdleConns"], 10, 32)
maxActives, _ := strconv.ParseInt(Config["mysql.maxActives"], 10, 32)
db.SetMaxIdleConns(int(maxIdleConns))
db.SetMaxOpenConns(int(maxActives))
}
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment