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.
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



