Skip to content

Instantly share code, notes, and snippets.

@djherbis
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djherbis/898283c55f1b58811840 to your computer and use it in GitHub Desktop.
Save djherbis/898283c55f1b58811840 to your computer and use it in GitHub Desktop.
Example djherbis/stow usage.
package main
import (
"fmt"
"log"
"github.com/boltdb/bolt"
"github.com/djherbis/stow"
)
type Name struct {
First string
Last string
}
func main(){
// Open Database
db, err := bolt.Open("my.db", 0600, nil)
if err != nil {
log.Fatal(err)
}
defer db.Close()
// Create a Store
// you will need to do gob.Register(Name{}) if you wish to use NewStore
store := stow.NewJSONStore(db, []byte("bucket"))
// Put a few names in the Store
store.Put([]byte("friend1"), &Name{"Friend A", "A Friend"})
store.Put([]byte("friend2"), &Name{"Friend B", "B Friend"})
// Get the names back
var friend1, friend2 Name
if store.Get([]byte("friend1"), &friend1) != stow.ErrNotFound {
fmt.Println(friend1)
}
if store.Get([]byte("friend2"), &friend2) != stow.ErrNotFound {
fmt.Println(friend2)
}
}
@joyrexus
Copy link

joyrexus commented Apr 8, 2015

Hmm. This example doesn't work if you instead use stow.NewStore (for gob en/decoding) unless you first register Name with gob. See this gist.

@djherbis
Copy link
Author

djherbis commented Apr 9, 2015

Thanks! That's actually a requirement of the Gob encoding, but you are correct so I'm adding a comment to clarify.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment