Skip to content

Instantly share code, notes, and snippets.

@Marvin9
Created August 21, 2020 09:13
Show Gist options
  • Save Marvin9/ece7053ddfc88a58b709b275c56bdab5 to your computer and use it in GitHub Desktop.
Save Marvin9/ece7053ddfc88a58b709b275c56bdab5 to your computer and use it in GitHub Desktop.
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