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.
go get -u github.com/giesan/store
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)
}
}
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)
}
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)
}
}
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)
}
Andrej Giesbrecht
store released under MIT license, refer LICENSE file.