Created
July 20, 2022 07:47
-
-
Save nidhi-canopas/2e2ab614146732fd2626e1ab118488d0 to your computer and use it in GitHub Desktop.
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 database | |
| import ( | |
| "fmt" | |
| "gorm.io/driver/mysql" | |
| "gorm.io/gorm" | |
| ) | |
| const DB_USERNAME = "root" | |
| const DB_PASSWORD = "root" | |
| const DB_NAME = "my_db" | |
| const DB_HOST = "127.0.0.1" | |
| const DB_PORT = "3306" | |
| var Db *gorm.DB | |
| func InitDb() *gorm.DB { | |
| Db = connectDB() | |
| return Db | |
| } | |
| func connectDB() (*gorm.DB) { | |
| var err error | |
| dsn := DB_USERNAME +":"+ DB_PASSWORD +"@tcp"+ "(" + DB_HOST + ":" + DB_PORT +")/" + DB_NAME + "?" + "parseTime=true&loc=Local" | |
| fmt.Println("dsn : ", dsn) | |
| db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) | |
| if err != nil { | |
| fmt.Println("Error connecting to database : error=%v", err) | |
| return nil | |
| } | |
| return db | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment