Skip to content

Instantly share code, notes, and snippets.

@ytnobody
Last active May 26, 2020 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ytnobody/d525b6b48393a67a4d472bf3e1e32879 to your computer and use it in GitHub Desktop.
Save ytnobody/d525b6b48393a67a4d472bf3e1e32879 to your computer and use it in GitHub Desktop.
こういうことをやりたい
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