Go's standard library handles SQL databases through database/sql, a driver-independent interface with connection pooling, and handles file and in-memory storage through os, bufio, encoding/json and sync. It does not include drivers or any NoSQL client, so you import a driver for your database, for example pgx for PostgreSQL or the official MongoDB driver.
Pick the storage by access pattern: a relational database for structured, transactional data, a key-value or document store for flexible or cached data, files for simple local persistence and plain maps with a lock for temporary state.
| Storage | Go approach | Good for |
|---|---|---|
| PostgreSQL, MySQL, SQLite | database/sql plus a driver | Transactions, relations, reporting |
| MongoDB, DynamoDB, Cassandra | Official or vendor SDK | Document or wide-column data |
| Redis | github.com/redis/go-redis/v9 | Caching, queues, sessions |
| Files | os, bufio, encoding/json, encoding/gob | Config, exports, small local data |
| Embedded key-value | bbolt, Badger | Local indexed storage without a server |
| In memory | map with sync.RWMutex, sync.Map | Caches and short-lived state |
Import a driver for its side effects, open a *sql.DB once, and share it. A sql.DB is a pool of connections that is safe for concurrent use, not a single connection, so do not open and close it per request.
Always use placeholders ($1 in PostgreSQL, ? in MySQL and SQLite) and never build SQL by joining user input, which prevents SQL injection. Always call rows.Close() and check rows.Err(). Use the Context variants so queries are cancelled with the request. sql.ErrNoRows signals that QueryRowContext(...).Scan found nothing, so test it with errors.Is.
Use a transaction when several statements must succeed or fail together. Deferring Rollback is safe because it does nothing after a successful Commit.
For nullable columns scan into sql.Null[T] (Go 1.22 and later) or sql.NullString and similar types.
The standard library has no NoSQL support. Use each vendor's client. The pattern is the same: create one long-lived client, pass a context.Context to every call and close the client on shutdown.
For MongoDB use the official go.mongodb.org/mongo-driver module and check its current major version for the import path and API, because they changed between v1 and v2.
For small amounts of local data, read and write files directly. Write to a temporary file and rename it, so a crash cannot leave a half-written file.
Use encoding/json for readable, portable data, encoding/csv for tables and encoding/gob only for Go-to-Go data. When you need queries or concurrent writers, step up to SQLite or an embedded store such as bbolt instead of inventing a file format.
A map is not safe for concurrent writes, so protect it with a mutex. A sync.RWMutex allows many readers at once.
This simple cache never evicts expired entries from memory. For production, add a cleanup goroutine or use a library with eviction. Data in memory is lost when the process restarts and is not shared across instances.
You do not need an ORM. Many Go teams prefer writing SQL and generating type-safe code from it.
| Option | Approach |
|---|---|
database/sql directly | Full control, more boilerplate |
sqlx | Thin helpers that scan rows into structs |
sqlc | Generates Go code from your SQL queries at build time |
| GORM | Full ORM with models, associations and hooks |
| Ent | Schema-as-code ORM with generated typed APIs |
AutoMigrate is convenient while prototyping, but production schemas are better managed with versioned migrations that you review.
database/sql include database drivers?No. It defines the interface. You import a driver such as pgx, go-sql-driver/mysql or modernc.org/sqlite and register it by name.
sql.DB a single connection?No. It is a concurrency-safe pool. Create it once per database and share it, and tune SetMaxOpenConns and SetConnMaxLifetime for your workload.
Only if it saves you time. sqlc or plain database/sql is common for performance and clarity, and GORM or Ent suit teams that want generated models and relations.
Use parameter placeholders in every query and pass values as arguments. Never concatenate user input into SQL.
Use database/sql with a driver for relational data, share one *sql.DB, pass contexts, use placeholders and wrap related changes in transactions. Use vendor clients for NoSQL stores, atomic file writes for small local data and a mutex-protected map for in-memory state. Choose an ORM or sqlc only when it reduces the code you maintain.