store

package
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 1, 2026 License: MIT Imports: 8 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

func IsNotExist(err error) bool

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 Dir

func Dir(dir string) Store

Dir returns a Store that keeps each entry as a file in dir, created on the first Save.

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

func Open(name string) (Store, error)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL