Skip to content

kv ​

go
import "github.com/tangoui/tango/service/kv"

本页由 site/tools/apigen 从源码注释生成(按 darwin 构建标签选文件),请勿手改。

Package kv is a small thread-safe key-value store for app settings and state: kept in memory and, optionally, persisted to a JSON file.

Values are anything encoding/json can encode. Once persisted and loaded again they come back as JSON decodes them into an any (numbers as float64, objects as map[string]any), so use the store for simple values.

A *Store can be bound to the page with bind.Register or service.Bind; its exported methods are Get, Has, Set, Delete, Keys and Clear.

函数 ​

func WriteFileAtomic ​

go
func WriteFileAtomic(path string, data []byte, perm fs.FileMode) (err error)

WriteFileAtomic writes data to path through a temporary file in the same directory that is synced and then renamed over path. Missing parent directories are created.

类型 ​

Store ​

go
type Store struct {
	// contains filtered or unexported fields
}

Store is a key-value store. Its methods are safe for concurrent use.

func New ​

go
func New(path string) (*Store, error)

New returns a store persisted to the JSON file at path, loading it if it exists. Every change rewrites the file atomically (a temporary file in the same directory renamed over it), so a crash leaves either the old or the new contents. An empty path returns a memory-only store.

func (*Store) Clear ​

go
func (s *Store) Clear() error

Clear removes every key.

func (*Store) Delete ​

go
func (s *Store) Delete(key string) error

Delete removes key. Deleting a missing key is not an error.

func (*Store) Get ​

go
func (s *Store) Get(key string) any

Get returns the value stored under key, or nil if there is none.

func (*Store) Has ​

go
func (s *Store) Has(key string) bool

Has reports whether a value is stored under key.

func (*Store) Keys ​

go
func (s *Store) Keys() []string

Keys returns the stored keys in sorted order.

func (*Store) Set ​

go
func (s *Store) Set(key string, value any) error

Set stores value under key. If the store is persisted and the file cannot be written, the store is left unchanged.

以 MIT 许可发布