In Go, data storage means writing data somewhere durable, and retrieval means reading it back in a usable form. The main techniques are flat files, structured encodings such as JSON, relational databases through database/sql, embedded key-value stores, and external caches or object stores. The right choice depends on data shape, size, concurrency and whether you need queries.
This guide compares the common options and shows working code for the ones you will use most.
| Technique | Best for | Go tools |
|---|---|---|
| Plain files | Logs, exports, config, small datasets | os, bufio, encoding/csv |
| JSON or binary encoding | Documents, snapshots, API payloads | encoding/json, encoding/gob |
| Relational database | Structured data, transactions, queries | database/sql with a driver such as pgx |
| Embedded key-value store | Local state, caches, no server | bbolt, badger, SQLite drivers |
| Object storage | Large blobs, media, backups | Cloud SDKs (S3 compatible) |
| In-memory cache | Fast repeated reads | sync.Map, Redis client |
Start with the simplest option that satisfies durability and query needs. Moving from a file to a database is easy if you hide storage behind an interface.
Write whole small files atomically so a crash never leaves a half-written file: write to a temp file in the same directory, then rename.
For large files, stream with bufio.Scanner or json.Decoder rather than reading everything into memory.
database/sql is a generic interface; you import a driver for your database. Always use placeholders, never string concatenation, to avoid SQL injection.
Points to remember:
sql.Open does not connect; the first query does. Call db.PingContext to check early.defer rows.Close() and check rows.Err() after the loop.context.Context so slow queries can be cancelled.db.BeginTx and commit or roll back.*sql.DB is a connection pool and is safe for concurrent use, so create it once.Libraries such as sqlc (generates typed Go from SQL) and sqlx reduce boilerplate while keeping SQL explicit.
When you need persistence without running a server, an embedded store is simple. bbolt is a B+tree key-value store in a single file.
Only one process can open a bbolt file at a time. SQLite through a Go driver is another strong choice when you want SQL with no server.
An interface keeps business logic independent of the backend and makes tests simple.
Provide one implementation for PostgreSQL and a small in-memory one for tests. Return a sentinel error such as ErrNotFound that callers check with errors.Is.
PostgreSQL is a common default for relational data. Use SQLite or bbolt for local, single-process state, and a cache such as Redis for hot data.
database/sql an ORM?No. It is a thin interface over SQL. ORMs such as GORM or Ent build on top of it if you want them.
Use placeholders ($1 for PostgreSQL, ? for MySQL and SQLite) and pass values as arguments. Never build queries with fmt.Sprintf from user input.
Small data is fine. Large files and media belong in object storage, with only their keys or URLs stored in the database.
Pick storage by data shape and access pattern: files and JSON for simple data, database/sql for relational data, embedded stores for local state, and object storage for blobs. Use contexts, placeholders, pooled connections and an interface boundary so retrieval stays safe, testable and easy to change.