persist

package
v0.0.0-...-2e2d98c Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2020 License: MIT Imports: 5 Imported by: 3

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var Marshal = func(v interface{}) (io.Reader, error) {
	b, err := json.MarshalIndent(v, "", "\t")
	if err != nil {
		return nil, err
	}
	return bytes.NewReader(b), nil
}

Marshal is a function that marshals the object into an io.Reader. By default, it uses the JSON marshaller.

View Source
var Unmarshal = func(r io.Reader, v interface{}) error {
	return json.NewDecoder(r).Decode(v)
}

Unmarshal is a function that unmarshals the data from the reader into the specified value. By default, it uses the JSON unmarshaller.

Functions

func Load

func Load(path string, v interface{}) error

Load loads the file at path into v. Use os.IsNotExist() to see if the returned error is due to the file being missing.

Example
defer os.Remove(storingFile)

ioutil.WriteFile(storingFile, []byte(value), 0666)

var fileLoaded interface{}
err := Load(storingFile, &fileLoaded)
if err != nil {
	log.Println(err.Error())
}
fmt.Println(fileLoaded)
Output:

map[foo:bar]

func Save

func Save(path string, v interface{}) error

Save saves a representation of v to the file at path.

Example
defer os.Remove(storingFile)

Save(storingFile, value)

fileRead, _ := ioutil.ReadFile(storingFile)
str := string(fileRead)
fmt.Printf(str)
Output:

"{\"foo\": \"bar\"}"

Types

This section is empty.

Jump to

Keyboard shortcuts

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