Go database/sql: a generic interface around SQL drivers
Answer: Package sql is a generic interface around SQL or SQL-like databases, and it must be paired with a database driver. QueryContext runs a query that returns rows (typically a SELECT). Args fill any placeholder parameters in that query.
Examples
package main
import (
"context"
"database/sql"
)
func list(ctx context.Context, db *sql.DB) (*sql.Rows, error) {
return db.QueryContext(ctx, "SELECT id FROM items")
}
Notes
Import a driver and then use a DB. If a driver does not support context cancellation, it returns only after the query is completed.
Reference list
Thesql package is a generic interface around SQL databases.Thepackage must be used with a database driver.QueryContextruns a query that returns rows, typically a SELECT.
See also
Claims
- id: C1 claim: The sql package is a generic interface around SQL databases. source: S1 evidence: "Package sql provides a generic interface around SQL"
- id: C2 claim: The package must be used with a database driver. source: S2 evidence: "The sql package must be used in conjunction with a database driver"
- id: C3 claim: QueryContext runs a query that returns rows, typically a SELECT. source: S2 evidence: "QueryContext executes a query that returns rows, typically a SELECT"
Sources
- [S1] database/sql package. Go docs. https://pkg.go.dev/database/sql . License: BSD-3-Clause. Accessed 2026-11-18.
- [S2] sql.go. Go source. https://raw.githubusercontent.com/golang/go/master/src/database/sql/sql.go . License: BSD-3-Clause. Accessed 2026-11-18.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Go docs, database/sql package; Go source, sql.go. 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: Go docs, database/sql package; Go source, sql.go. BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors.
Cite this
iinventedlight. 2026. "Go database/sql: a generic interface around SQL drivers." iinventedlight Library, iil:note:479akph7, version of 2026-11-18. https://iinventedlight.com/library/notes/go-database-sql/. License: CC-BY-4.0.
@misc{iil_note_479akph7,
author = {{iinventedlight}},
title = {{Go database/sql: a generic interface around SQL drivers}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/go-database-sql/},
note = {Permanent ID iil:note:479akph7. License: CC-BY-4.0.},
}
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.
- go-database-sql.table.json (table; CC-BY-4.0)
- go-database-sql.schema.json (schema; CC-BY-4.0)
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.md.