Skip to content

Instantly share code, notes, and snippets.

@darmawan01
Last active May 16, 2020 14:10
Show Gist options
  • Save darmawan01/c504d9964e47bf6d2cc1704582329043 to your computer and use it in GitHub Desktop.
Save darmawan01/c504d9964e47bf6d2cc1704582329043 to your computer and use it in GitHub Desktop.
Setup Mysql Connection With Gorm
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