← All notes

Go database/sql/driver: interfaces implemented by database drivers

Answer: The driver package defines interfaces that a database driver must implement for database/sql; most programs should import database/sql instead of this package.

Which interfaces a driver should implement

The driver interface has changed over time.

Drivers should implement Connector and DriverContext.

The Connect and Open methods on Connector and Driver should never return ErrBadConn.

ErrBadConn is only appropriate from Validator, SessionResetter, or a query method when the connection is already invalid, for example already closed.

Even if the server sends back an error, you should not return ErrBadConn, and errors are checked with errors.Is.

Every Conn should implement Pinger, SessionResetter, and Validator.

If the driver supports named parameters or context, Conn should also implement ExecerContext, QueryerContext, ConnPrepareContext, and ConnBeginTx.

Implement NamedValueChecker when you need custom data types.

CheckNamedValue can also drop a per-query option by returning ErrRemoveArgument.

Drivers that expose more than one result set should implement RowsNextResultSet on Rows.

A row value may itself return a Rows type, and that can represent a database cursor value.

Pooling, sessions, and value conversion

When a Conn implements Validator, IsValid is called before the connection is returned to the pool.

When a pool entry implements SessionResetter, ResetSession is called before that connection is reused for another query.

If a connection is reused immediately and never returns to the pool, ResetSession still runs, but IsValid does not.

Bool is a ValueConverter that converts input values to bool.

DefaultParameterConverter returns the argument unchanged when IsValue(arg) is true.

Otherwise, if the argument implements Valuer, its Value method supplies the Value.

If the argument is a nil pointer, ConvertValue returns a nil Value. If the also argument is a non-nil pointer, it is dereferenced and ConvertValue is called recursively.

See also

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: The Go Authors, driver package - database/sql/driver; The Go Authors, driver.go (source). BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors. Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: The Go Authors, driver package - database/sql/driver; The Go Authors, driver.go (source). BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors.

Cite this

Permanent ID: iil:note:m4cx9thz

iinventedlight. 2026. "Go database/sql/driver: interfaces implemented by database drivers." iinventedlight Library, iil:note:m4cx9thz, version of 2026-11-26. https://iinventedlight.com/library/notes/go-database-sql-driver/. License: CC-BY-4.0.

@misc{iil_note_m4cx9thz,
  author = {{iinventedlight}},
  title = {{Go database/sql/driver: interfaces implemented by database drivers}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-26},
  url = {https://iinventedlight.com/library/notes/go-database-sql-driver/},
  note = {Permanent ID iil:note:m4cx9thz. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

Extra files beside this note

These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.

All artifact files (JSON index)

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/go-database-sql-driver.md.