Documentation
¶
Overview ¶
Package store keeps an app's persistent data: small named entries, such as settings, a saved game or a sync token, each read and replaced whole.
Open gives the platform's place for app data, so an app never looks for a config directory itself (Android has none):
st, err := store.Open("myapp")
if err != nil {
log.Printf("settings won't be saved: %v", err)
st = store.Memory()
}
b, err := st.Load("settings.json") // fs.ErrNotExist before the first Save
...
err = st.Save("settings.json", b)
Tests use Dir(t.TempDir()) to persist across a simulated restart, or Memory() for state that doesn't need to outlive the test.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsNotExist ¶
IsNotExist reports whether err is Load's "never saved" error.
Types ¶
type Store ¶
type Store interface {
// Load returns the entry's contents, or an error matching
// fs.ErrNotExist (errors.Is) if it was never saved.
Load(name string) ([]byte, error)
// Save replaces the entry with b, all at once: a crash or power loss
// mid-save leaves the old contents or the new, never a mix.
Save(name string, b []byte) error
}
Store holds named entries. Names are plain file names ("save.json", "volume"): no path separators, and not "." or "..". Implementations are safe from any goroutine.
func Memory ¶
func Memory() Store
Memory returns an empty Store that keeps entries in memory, for tests and for running on when Open fails. Load and Save copy, so callers may reuse their slices.
func Open ¶
Open returns the Store for the app called name: a directory named name in the platform's app data directory (Gio's app.DataDir). That is os.UserConfigDir on desktops (~/.config/name on Linux), the app's private files directory on Android, and its Documents directory on iOS. In a browser it is localStorage, each entry under the key "name/entry". Name is usually the app's short name ("myapp"); keep it once released, or saved data is left behind.
On Android, call Open from main or later, never from a package init.