liveBook · Manning

Database driver

As explained in Go by Example

A database driver is software that implements a database access interface for communicating with a particular database technology.

A database driver implements the database-specific side of a common access API. In Go, application code usually uses database/sql, while a driver communicates with SQLite, PostgreSQL, or another SQL database through database/sql/driver interfaces. This keeps query code centered on *sql.DB rather than on a database vendor’s API. section 10.1

Figure 10.1 The sql package abstracts underlying databases with drivers. The sql package’s registry stores registered drivers, and *DB manages a connection pool.
A computer screen shot of a computer

AI-generated content may be incorrect.

The registry selects a registered driver; *sql.DB then manages pooled connections while the driver handles database-specific communication. section 10.1

Use a driver through a small, explicit setup flow:

  • Add a driver module to the Go module. The standard library supplies the abstraction but does not bundle SQL drivers. section 10.1.1
  • Register the driver, commonly with a blank import such as _ "modernc.org/sqlite"; its initialization adds the driver under its name. section 10.1.1
  • Call sql.Open with the registered driver name and a driver-specific data source name (DSN). Open checks the registry and returns a *sql.DB handle. section 10.1
  • Call PingContext to verify that a connection can be established; Open may return a DB whose pool has no active connections yet. section 10.1.2
  • Keep the *sql.DB for the program lifetime so its pool can reuse connections instead of reconnecting for every query. section 10.1.2

Isolate SQLite behind a package

A sqlite package can blank-import modernc.org/sqlite and expose Dial(ctx, dsn). Dial calls sql.Open("sqlite", dsn), wraps any opening error, then calls db.PingContext(ctx) and wraps any connection error. Other packages depend on this entry point rather than scattering SQLite-specific setup throughout the application. section 10.1.1 section 10.1.2

Driver choice can affect deployment. modernc.org/sqlite is presented as a pure-Go option. mattn/go-sqlite3 offers performance and features but uses C bindings; enabling CGO can complicate cross-compilation, slow builds, and add external dependencies. section 10.1.1

A *sql.DB is a concrete type that gains database polymorphism through driver interfaces. It also manages retries and a pool of idle and in-use connections. Pool limits and connection lifetime settings are available, but poor values can hurt performance. section 10.1 section 10.1.2

How Go by Example covers it

How deep it goes
Explains registration, Open, PingContext, pooling, and tuning knobs, while deferring driver implementation unless building a driver.
Examples
Demonstrates modernc.org/sqlite registration, sql.Open("sqlite", "file:links.db"), and a Dial helper that calls PingContext.
What it assumes
Assumes Go modules and imports, then teaches SQL-driver installation, blank-import registration, DSNs, and pooled connection setup from zero.
What you can do after
Apply it via database/sql to register a SQLite driver, open a pooled database handle, and verify connectivity with PingContext.
How it connects
Connects database drivers with database/sql, database/sql/driver, *sql.DB, DSNs, connection pools, CGO, SQLite, and PostgreSQL.

Also explained in

Compare how 4 books explain database driver →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go by Example. The section links point to the parts of the book it draws on.