Last active
May 16, 2020 14:10
-
-
Save darmawan01/c504d9964e47bf6d2cc1704582329043 to your computer and use it in GitHub Desktop.
Setup Mysql Connection With Gorm
This file contains hidden or 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 db | |
import ( | |
"fmt" | |
"github.com/jinzhu/gorm" | |
_ "github.com/jinzhu/gorm/dialects/mysql" | |
) | |
// InitMysql func | |
func InitGormMysql() (conn *gorm.DB) { | |
conf := getConfig() | |
var err error | |
conn, err = gorm.Open("mysql", | |
fmt.Sprintf("%s:%s@(%s:%d)/%s?charset=utf8&parseTime=True&loc=Local", | |
conf.User, | |
conf.Password, | |
conf.Host, | |
conf.Port, | |
conf.DbName, | |
), | |
) | |
if err != nil { | |
panic(err) | |
} | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment