Skip to content

Instantly share code, notes, and snippets.

@joyrexus
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save joyrexus/d799527b7b3762784b35 to your computer and use it in GitHub Desktop.
Save joyrexus/d799527b7b3762784b35 to your computer and use it in GitHub Desktop.
stowing gobs

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 interface{}
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(i interface{}) {
Friends = append(Friends, i.(Name))
})
fmt.Println(Friends)
}
@djherbis
Copy link

djherbis commented Apr 9, 2015

I've made some alterations to the library, here's an updated version of this code:
https://gist.github.com/djherbis/e01d260db35d37c969cc

@djherbis
Copy link

djherbis commented Apr 9, 2015

The changes are small, friend1, friend2 of type Name, and the function that ForEach now takes a Name as a parameter instead of interface{}

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