store

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2021 License: MIT Imports: 6 Imported by: 0

README

Store - Read and write data structures

Store provides the ability to write the data structures to a file and read from a file in the Go programming language.

The module provides possibility of the data structures in a JSON format or in a binary format. During the writing or reading process the file is locked.

Reading/writing in JSON format were implemented with the standard library encoding/json.

For reading/writing in binary format the module is encoding/gob used.

Usage

Writing in JSON format to a file:

type message struct {
	Name      string
	Timestamp time.Time
	Payload   []byte
	Ssid      []uint32
}

msg := &message{
    Name:      "John",
    Timestamp: time.Now(),
    Payload:   []byte("hi"),
    Ssid:      []uint32{1, 2, 3},
}

if err := WriteJSON("./temp.json", msg); err != nil {
    log.Fatalln(err)
}

Read from a file written in JSON format:

var msg message
if err := ReadJSON("./temp.json", &msg); err != nil {
    log.Fatalln(err)
}

fmt.Println(msg.Name) // Output John

Writing in binary format to a file:

type message struct {
	Name      string
	Timestamp time.Time
	Payload   []byte
	Ssid      []uint32
}

msg := &message{
    Name:      "John",
    Timestamp: time.Now(),
    Payload:   []byte("hi"),
    Ssid:      []uint32{1, 2, 3},
}

if err := WriteBinary("./temp.bin", msg); err != nil {
    log.Fatalln(err)
}

Read from a file written in binary format:

var msg message
if err := ReadBinary("./temp.bin", &msg); err != nil {
    log.Fatalln(err)
}
fmt.Println(msg.Name) // Output John

Copyright (c) 2021 Andrej Giesbrecht

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReadBinary

func ReadBinary(file string, v interface{}) error

ReadBinary loads the file into v and returns an error, if any.

func ReadJSON

func ReadJSON(file string, v interface{}) error

ReadJSON loads the file into v and returns an error, if any.

func WriteBinary

func WriteBinary(file string, v interface{}) (int64, error)

WriteBinary saves a representation of v to the file at path. It returns the number of bytes written and an error, if any.

func WriteJSON

func WriteJSON(file string, v interface{}) (int64, error)

WriteJSON saves a representation of v to the file. It returns the number of bytes written and an error, if any.

Types

This section is empty.

Jump to

Keyboard shortcuts

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