Skip to content
/ stow Public
forked from djherbis/stow

Simple object persistence with boltdb

License

Notifications You must be signed in to change notification settings

PelionIoT/stow

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

82 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Stow

GoDoc Release Software License Build Status Coverage Status Go Report Card

Usage

This package provides a persistence manager for objects backed by boltdb.

package main

import (
  "encoding/gob"
  "fmt"
  "log"

  "github.com/boltdb/bolt"
  "gopkg.in/djherbis/stow.v2"
)

func main() {
  // Create a boltdb database
  db, err := bolt.Open("my.db", 0600, nil)
  if err != nil {
    log.Fatal(err)
  }

  // Open/Create a Json-encoded Store, Xml and Gob are also built-in
  // We'll we store a greeting and person in a boltdb bucket named "people"
  peopleStore := stow.NewJSONStore(db, []byte("people"))

  peopleStore.Put("hello", Person{Name: "Dustin"})

  peopleStore.ForEach(func(greeting string, person Person) {
    fmt.Println(greeting, person.Name)
  })

  // Open/Create a Gob-encoded Store. The Gob encoding keeps type information,
  // so you can encode/decode interfaces!
  sayerStore := stow.NewStore(db, []byte("greetings"))

  var sayer Sayer = Person{Name: "Dustin"}
  sayerStore.Put("hello", &sayer)

  var retSayer Sayer
  sayerStore.Get("hello", &retSayer)
  retSayer.Say("hello")

  sayerStore.ForEach(func(sayer Sayer) {
    sayer.Say("hey")
  })
}

type Sayer interface {
  Say(something string)
}

type Person struct {
  Name string
}

func (p Person) Say(greeting string) {
  fmt.Printf("%s says %s.\n", p.Name, greeting)
}

func init() {
  gob.Register(&Person{})
}

Installation

go get gopkg.in/djherbis/stow.v2

About

Simple object persistence with boltdb

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%