sql

More About SQL With Go

package main import ( "database/sql" "fmt" _ "github.com/jackc/pgx/v4/stdlib" // stdlib is the package that provides the standard library driver. To be able to use database sql package ) type PostgresConfig struct { Host string Port string User string Password string Database string SSLMode string } func (cfg PostgresConfig) String() string { return fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=%s", cfg.Host, cfg.Port, cfg.User, cfg.Password, cfg.Database, cfg.SSLMode) } func main() { cfg := PostgresConfig{ Host: "localhost", Port: "5432", User: "baloo", Password: "junglebook", Database: "lenslocked", SSLMode: "disable", } // pgx = Driver name defined by github.

Executing SQL With Go

The first difference in this piece of code is that the “PostgresConfig type” is replacing the long string in the sql.Open() section, as it was in the previous version of the code. The second and most important difference is that we are using db.Exec(). As far as I understand, it seems that db.Exec is used when the SQL query does not return rows, for example when doing “delete”, “update”, “insert”. On the other hand, when requiring a “select”, then db.