Created
August 21, 2020 09:13
-
-
Save Marvin9/ece7053ddfc88a58b709b275c56bdab5 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 database | |
import ( | |
"fmt" | |
"log" | |
"os" | |
"github.com/jinzhu/gorm" | |
) | |
// ConnectDB - It will be used anywhere in application to connect database. | |
func ConnectDB() (*gorm.DB, error) { | |
databaseURL := os.Getenv("DATABASE_URL") | |
databaseName := os.Getenv("DATABASE_NAME") | |
db, err := gorm.Open("postgres", fmt.Sprintf("%v/%v", databaseURL, databaseName)) | |
if err != nil { | |
log.Printf("Error connecting database.\n%v", err) | |
return nil, err | |
} | |
return db, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment