Go persists data in three main ways: files on disk (using os, bufio, and encoders such as encoding/json, encoding/csv, and encoding/gob), embedded databases such as SQLite or BoltDB-style key-value stores, and client-server databases accessed through database/sql or a dedicated client. The right choice depends on data size, concurrency, query needs, and durability.
This guide compares these options and shows reliable patterns for writing data safely, since the most common persistence bugs are partial writes and unclosed files.
| Approach | Best for | Notes |
|---|---|---|
| JSON or CSV file | Config, exports, small datasets | Human readable, no queries |
encoding/gob | Go-to-Go local serialisation | Go-specific, not for sharing |
| Protocol Buffers | Typed, compact, cross-language data | Needs generated code |
| SQLite | Single-node apps, tools, tests | Embedded SQL, one file |
| Embedded key-value store | Fast local lookups, caches | For example bbolt, Badger, Pebble |
| PostgreSQL or MySQL | Multi-user services, relational data | Use database/sql |
| Redis, DynamoDB, MongoDB | Caches, key-value, documents | Use official clients |
| Object storage (S3-compatible) | Large blobs and backups | Use the provider SDK |
Start with the simplest tier that meets durability and concurrency needs, and move up when you need queries, multiple writers, or replication.
Marshal a struct to JSON and write it to disk. Check every error, because disks fill up and permissions change.
os.ReadFile and os.WriteFile replace the old ioutil helpers, which are deprecated. Use 0o600 for files that may contain secrets.
os.WriteFile truncates the destination first, so a crash mid-write leaves a corrupt file. For important data, write to a temporary file in the same directory, flush it to disk, then rename it over the target. A rename within one filesystem is atomic on POSIX systems.
For append-only logs, open with os.O_APPEND|os.O_CREATE|os.O_WRONLY and call Sync when durability matters.
SQLite gives you SQL, transactions, and a single file with no server to run. Use it through database/sql with either a cgo driver such as mattn/go-sqlite3 or a pure-Go driver such as modernc.org/sqlite, which avoids a C toolchain.
WAL mode and a busy timeout help with concurrent readers. SQLite allows one writer at a time, so limit writers (for example db.SetMaxOpenConns(1) for write-heavy workloads).
For services with many users, use PostgreSQL or MySQL through database/sql with pgx or another driver. Pass a context into QueryContext and ExecContext, use placeholders for every value, and wrap multi-step changes in a transaction. Manage schema changes with migration files rather than ad hoc CREATE TABLE calls at startup.
When you only need get, put, and range scans, a key-value store such as bbolt or Badger is simpler and faster than SQL. For shared caches across processes, use Redis with a client such as go-redis, and always set expirations on cache keys.
Close on files you wrote.Sync, rename).Marshal a struct with encoding/json and write it with os.WriteFile. It is fine for small data written by one process.
Use JSON for readable or shared files. Use gob only between Go programs, and remember it is not designed as a long-term archive format.
Yes for single-node apps and read-heavy workloads when you enable WAL and handle the single-writer limit. Use PostgreSQL or MySQL if you need many concurrent writers or multiple app servers.
Its functions moved to os and io (os.ReadFile, os.WriteFile, io.ReadAll) in Go 1.16. The old package still compiles but should not be used in new code.
Pick storage by need: files for small data, SQLite for embedded SQL, PostgreSQL or MySQL for multi-user services, key-value stores or Redis for fast lookups. Always handle errors, write files atomically, use transactions, and plan for backups and migrations.