Skip to content

Instantly share code, notes, and snippets.

@djherbis
Forked from joyrexus/README.md
Last active August 29, 2015 14:18
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/e01d260db35d37c969cc to your computer and use it in GitHub Desktop.
Save djherbis/e01d260db35d37c969cc to your computer and use it in GitHub Desktop.

Looks like you have to use gob to register the types you want to stow when using stow.NewStore.

Compare original example using stow.NewJsonStore.

See this post for context.

package main
import (
"fmt"
"log"
"encoding/gob"
"github.com/boltdb/bolt"
"github.com/djherbis/stow"
)
type Name struct {
First string
Last string
}
func main() {
gob.Register(Name{}) // gob: type not registered for interface: main.Name
// Open Database
db, err := bolt.Open("my.db", 0600, nil)
if err != nil {
log.Fatal(err)
}
defer db.Close()
// Create a Store
store := stow.NewStore(db, []byte("bucket"))
// Put a few names in the Store
err = store.Put([]byte("friend1"), &Name{"Friend A", "A Friend"})
if err != nil {
fmt.Println(err)
}
store.Put([]byte("friend2"), &Name{"Friend B", "B Friend"})
if err != nil {
fmt.Println(err)
}
// 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)
}
var Friends []Name
store.ForEach(func(name Name) {
Friends = append(Friends, name)
})
fmt.Println(Friends)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment