DEV Community

Discussion on: How I Built an API with Mux, Go, PostgreSQL, and GORM

Collapse
 
andres profile image
Andrés Pérez • Edited

I would suggest using Sprintf instead of concatenating your strings for the database connection, nonetheless, good job! working with Go is amazingly fun.

Collapse
 
aspittel profile image
Ali Spittel

Agreed!

Collapse
 
tangzero profile image
Jairinho • Edited

In this case, I think os.ExpandEnv is a better solution:

db, err = gorm.Open(
        "postgres",
        os.ExpandEnv("host=${HOST} user=${USER} dbname=${DBNAME} sslmode=disable password=${PASSWORD}"))
Enter fullscreen mode Exit fullscreen mode
Collapse
 
andres profile image
Andrés Pérez

If you're defining the environment variables, yeah, sure.

Collapse
 
aspittel profile image
Ali Spittel

Oh awesome, didn't know about this -- will definitely try it!