Documentation ¶
Overview ¶
Package locstor provides gopherjs bindings for the localStorage API. It allows you to store and retrieve any arbitrary go data structure, and is intended to be compiled to javascript with gopherjs and run in the browser.
Version 0.2.2 ¶
For the full source code, example usage, and more information visit https://github.com/go-humble/locstor.
Index ¶
- Variables
- func Clear() (err error)
- func DetectStorage() (ok bool)
- func GetItem(key string) (s string, err error)
- func Key(item string) (s string, err error)
- func Length() (l int, err error)
- func RemoveItem(key string) (err error)
- func SetItem(key, item string) (err error)
- type DataStore
- type Decoder
- type Encoder
- type EncoderDecoder
- type ItemNotFoundError
Constants ¶
This section is empty.
Variables ¶
var ( // BinaryEncoding is a ready-to-use implementation of EncoderDecoder which // encodes data structures in a binary format using the gob package. BinaryEncoding = &binaryEncoderDecoder{} // JSONEncoding is a ready-to-use implementation of EncoderDecoder which // encodes data structures as json. JSONEncoding = &jsonEncoderDecoder{} )
var ErrLocalStorageNotSupported = errors.New("localStorage does not appear to be supported/enabled in this browser")
ErrLocalStorageNotSupported is returned if localStorage is not supported.
Functions ¶
func DetectStorage ¶ added in v0.2.0
func DetectStorage() (ok bool)
DetectStorage detects and (re)initializes the localStorage.
func GetItem ¶
GetItem finds and returns the item identified by key. If there is no item in localStorage with the given key, GetItem will return an ItemNotFoundError.
func Key ¶
Key finds and returns the key associated with the given item. If the item is not in localStorage, Key will return an ItemNotFoundError.
func RemoveItem ¶
RemoveItem removes the item with the given key from localStorage.
Types ¶
type DataStore ¶
type DataStore struct {
Encoding EncoderDecoder
}
DataStore is an object with methods for storing and retrieving arbitrary go data structures in localStorage.
func NewDataStore ¶
func NewDataStore(encoding EncoderDecoder) *DataStore
NewDataStore creates and returns a new DataStore with the given encoding. locstor.JSON and locstor.Binary are two encodings provided by default. You can also pass in a custom encoding.
type Decoder ¶
Decoder is an interface implemented by objects which can decode a slice of bytes into an arbitrary go object.
type Encoder ¶
Encoder is an interface implemented by objects which can encode an arbitrary go object into a slice of bytes.
type EncoderDecoder ¶
EncoderDecoder is an interface implemented by objects which can both encode an arbitrary go object into a slice of bytes and decode that slice of bytes into an arbitrary go object. EncoderDecoders should have the property that Encode(Decode(x)) == x for all objects x which are encodable.
type ItemNotFoundError ¶
type ItemNotFoundError struct {
// contains filtered or unexported fields
}
ItemNotFoundError is returned if an item with the given key does not exist in localStorage.
func (ItemNotFoundError) Error ¶
func (e ItemNotFoundError) Error() string
Error implements the error interface.