Skip to content

Instantly share code, notes, and snippets.

@ytnobody
Last active May 26, 2020
Embed
What would you like to do?
こういうことをやりたい
package main
import (
"database/sql"
_ "github.com/go-sql-driver/mysql"
"github.com/ytnobody/gomysqlerror"
"github.com/volatiletech/sqlboiler/boil"
)
func ConnectDB(d config.DataSource) {
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", d.User, d.Password, d.Host, d.Port, d.DBName)
con, err := sql.Open(d.Driver, dsn)
if err != nil {
// 現状ではGopherの人はここら辺で文字列マッチングをかけているみたい。Typoしたらどーすんのっと。
mysqlErr := gomysqlerror.Isa(err) // Isaって名前はもう少しなんとかならんものかと悩み中
switch mysqlErr.Symbol {
case "EE_XXX":
...
case "EE_HOGEHOGE":
...
default:
...
}
}
con.SetMaxIdleConns(10)
con.SetMaxOpenConns(10)
con.SetConnMaxLifetime(300 * time.Second)
boil.SetDB(con)
boil.DebugMode = true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment