Go's standard library handles SQL data storage through the database/sql package, which defines a driver-independent interface for connecting, querying and running transactions. You import a database driver (for example a PostgreSQL or MySQL driver) for its side effects, open a *sql.DB handle that manages a connection pool, and run queries with context-aware methods. For file-based and simple storage, os, encoding/json, encoding/gob and io/fs cover the rest.
The database/sql package contains no drivers. The standard library provides the interface, and a separate module implements it for each database.
ExecContextsql.Open does not connect. It validates its arguments and returns a pool that is safe for concurrent use. Create it once at startup, share it and call PingContext to verify connectivity.
The blank import registers the driver under the name used in sql.Open. Never open a new *sql.DB per request. Tune the pool with SetMaxOpenConns, SetMaxIdleConns and SetConnMaxLifetime to match your database limits. Keep credentials in environment variables or a secret store, not in source code.
Use placeholders for every value so the driver sends parameters separately from the SQL text. This prevents SQL injection. The placeholder syntax depends on the driver: ? for MySQL and SQLite, $1 for PostgreSQL.
Always defer rows.Close() and check rows.Err() after the loop, because an error can end iteration early. Use sql.Null* types (or pointers) for nullable columns. Compare against sql.ErrNoRows with errors.Is in new code.
ExecContextExecContext runs statements that return no rows and reports the affected rows and last insert ID where the driver supports it.
On PostgreSQL, use INSERT ... RETURNING id with QueryRowContext instead of LastInsertId.
Group statements that must succeed or fail together in a transaction. Roll back on every error path.
Deferring Rollback is the safe pattern: it undoes the work on any early return and does nothing after Commit. Keep transactions short, because each one holds a connection from the pool.
| Need | Package | Notes |
|---|---|---|
| Relational databases | database/sql plus a driver | Pooling, transactions, prepared statements |
| Config and small documents | encoding/json, os | Write to a temp file, then os.Rename for atomic replacement |
| Go-only binary persistence | encoding/gob | Not portable to other languages |
| Tabular exports | encoding/csv | Streaming reader and writer |
| Embedded read-only data | embed | Compiled into the binary |
| In-memory cache | sync.Map, map with sync.RWMutex | Not durable |
Go has no built-in key-value store or embedded SQL engine. SQLite, Redis and document databases all need external drivers.
No. database/sql only defines the interface. Import a driver module for your database, usually with a blank import.
It is optional. database/sql is enough for many services. Code generators such as sqlc or libraries such as sqlx reduce boilerplate while keeping SQL explicit.
sql.Open not return an error for a wrong password?It does not connect. Call db.PingContext at startup to verify credentials and reachability.
Pass user input as arguments to placeholders and never build SQL by concatenating strings.
Use database/sql with a driver, one shared *sql.DB pool, context-aware methods, placeholders for every value and defer rows.Close() with a rows.Err() check. Wrap multi-statement changes in transactions with a deferred Rollback. For non-SQL needs, the standard library offers files, JSON, gob and CSV, while richer stores require third-party drivers.