Last active
August 29, 2015 14:16
-
-
Save djherbis/898283c55f1b58811840 to your computer and use it in GitHub Desktop.
Example djherbis/stow usage.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
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
Hmm. This example doesn't work if you instead use
stow.NewStore
(for gob en/decoding) unless you first registerName
with gob. See this gist.