Created
October 31, 2016 22:19
-
-
Save JeffreyBPetersen/5a4f54e3820020f39bfa052db892cc66 to your computer and use it in GitHub Desktop.
BoltDB example
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" | |
) | |
func main(){ | |
fmt.Println("hi there") | |
// open the database file | |
db, err := bolt.Open("tdbpi.boltdb", 0600, nil) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer db.Close() | |
// begin an interaction with the database | |
db.Update(func(tx *bolt.Tx) error { | |
// create bucket if needed | |
b, err := tx.CreateBucketIfNotExists([]byte("tbpi")) | |
if err != nil { | |
return fmt.Errorf("create bucket: %s", err) | |
} | |
// set a value in the bucket | |
err = b.Put([]byte("tkpi"), []byte("tvpi")) | |
return err | |
}) | |
// get back the value in a separate and read-only interaction | |
db.View(func(tx *bolt.Tx) error { | |
b := tx.Bucket([]byte("tbpi")) | |
v := b.Get([]byte("tkpi")) | |
fmt.Printf("please ignore: %s\n", v) | |
return nil | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment