Last active
May 26, 2020 15:39
-
-
Save ytnobody/d525b6b48393a67a4d472bf3e1e32879 to your computer and use it in GitHub Desktop.
こういうことをやりたい
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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