0PricingLogin
Go Academy · Lesson

database/sql and Drivers

DB setup, QueryRow, Scan, and connection pools

database/sql overview

database/sql is Go's standard database abstraction layer. It manages a connection pool and works with any database via a driver plugin.

Registering a driver

Import the driver package for its side effects (driver registration). Then open a connection:

import _ "github.com/lib/pq" // PostgreSQL driver

db, err := sql.Open("postgres", "postgres://user:pass@localhost/mydb?sslmode=disable")
if err != nil { log.Fatal(err) }
defer db.Close()

All lessons in this course

  1. database/sql and Drivers
  2. sqlx: Struct Scanning and Named Queries
  3. pgx: High-Performance PostgreSQL Driver
  4. Transactions and Migrations
← Back to Go Academy