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.
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) | |
| } |
The changes are small, friend1, friend2 of type Name, and the function that ForEach now takes a Name as a parameter instead of interface{}