September 2017
Beginner to intermediate
304 pages
7h 2m
English
The first thing we need do before connecting to an SQL-like database is identify the particular database that we will be interacting with and import a corresponding driver. In the following examples, we will be connecting to a Postgres database and will utilize the github.com/lib/pq database driver for database/sql. This driver can be loaded via an empty import (with a corresponding comment):
import ( "database/sql" "fmt" "log" "os" // pq is the library that allows us to connect // to postgres with databases/sql. _ "github.com/lib/pq")
Now let's assume that you have exported the Postgres connection string to an environmental variable PGURL. We can easily create an sql.DB value for our connection via the follow ...
Read now
Unlock full access