How to Use Connection String DSN in Go

Go uses URL-style connection strings passed directly to sql.Open instead of system DSNs.

Go does not use DSN (Data Source Name) connection strings; it uses standard URL-style connection strings directly in driver-specific functions. Pass your database credentials and host details as a single string argument to sql.Open or the driver's initialization function.

import "database/sql"
import _ "github.com/lib/pq"

connStr := "host=localhost user=myuser password=mypass dbname=mydb sslmode=disable"
db, err := sql.Open("postgres", connStr)
if err != nil {
    panic(err)
}
defer db.Close()

For MySQL, the format is user:password@tcp(host:port)/dbname.