Last active
February 3, 2017 16:33
-
-
Save sdomino/9ada44dc756476dd741d to your computer and use it in GitHub Desktop.
A small JSON database in Golang - Scribble: https://medium.com/@skdomino/scribble-a-tiny-json-database-in-golang-9817854deb05
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 ( | |
"encoding/json" | |
"fmt" | |
"github.com/nanobox-io/golang-scribble" | |
) | |
// a fish | |
type Fish struct{ Name string } | |
func main() { | |
// create a new scribble database, providing a destination for the database to live | |
db, _ := scribble.New("./fish", nil) | |
// add some fish to the database | |
for _, name := range []string{"onefish", "twofish", "redfish", "bluefish"} { | |
db.Write("fish", name, Fish{Name: name}) | |
} | |
// Read one fish from the database | |
onefish := Fish{} | |
db.Read("fish", "onefish", &onefish) | |
fmt.Printf("It's a fish! %#v\n", onefish) | |
// Read more fish from the database | |
morefish, _ := db.ReadAll("fish") | |
// iterate over morefish creating a new fish for each record | |
fishies := []Fish{} | |
for _, fish := range morefish { | |
f := Fish{} | |
json.Unmarshal([]byte(fish), &f) | |
fishies = append(fishies, f) | |
} | |
fmt.Printf("It's a lot of fish! %#v\n", fishies) | |
// Delete onefish from the database | |
db.Delete("fish", "onefish") | |
// Delete all fish from the database | |
db.Delete("fish", "") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment