Skip to content

giesan/store

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

This module was inspired by Perl Storable. 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.

The module encoding/gob is used for reading/writing in binary format.

Install

go get -u github.com/giesan/store

Usage

Writing in JSON format to a file:

package main

import (
	"log"
	"time"

	"github.com/giesan/store"
)

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

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

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

Read from a file written in JSON format:

package main

import (
	"log"
	"time"

	"github.com/giesan/store"
)

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

func main() {
	var msg message

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

	log.Println(msg.Name)
}

Writing in binary format to a file:

package main

import (
	"log"
	"time"

	"github.com/giesan/store"
)

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

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

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

Read from a file written in binary format:

package main

import (
	"log"
	"time"

	"github.com/giesan/store"
)

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

func main() {
	var msg message

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

	log.Println(msg.Name)
}

Author

Andrej Giesbrecht

LICENSE

store released under MIT license, refer LICENSE file.

About

Store - Read and write data structures

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages