Merge pull request #17 from vhugo/16-fix-readme-example
Fix README example
This commit is contained in:
commit
ced58d6718
14
README.md
14
README.md
@ -24,8 +24,8 @@ if err := db.Write("fish", "onefish", fish); err != nil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Read a fish from the database (passing fish by reference)
|
// Read a fish from the database (passing fish by reference)
|
||||||
fish := []Fish{}
|
onefish := Fish{}
|
||||||
if err := db.Read("fish", "onefish", &fish); err != nil {
|
if err := db.Read("fish", "onefish", &onefish); err != nil {
|
||||||
fmt.Println("Error", err)
|
fmt.Println("Error", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,9 +35,13 @@ if err != nil {
|
|||||||
fmt.Println("Error", err)
|
fmt.Println("Error", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fish := []Fish{}
|
fishies := []Fish{}
|
||||||
if err := json.Unmarshal([]byte(records), &fish); err != nil {
|
for _, f := range records {
|
||||||
fmt.Println("Error", err)
|
fishFound := Fish{}
|
||||||
|
if err := json.Unmarshal([]byte(f), &fishFound); err != nil {
|
||||||
|
fmt.Println("Error", err)
|
||||||
|
}
|
||||||
|
fishies = append(fishies, fishFound)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete a fish from the database
|
// Delete a fish from the database
|
||||||
|
58
example/fishing.go
Normal file
58
example/fishing.go
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
scribble "github.com/nanobox-io/golang-scribble"
|
||||||
|
)
|
||||||
|
|
||||||
|
// a fish
|
||||||
|
type Fish struct{ Name string }
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
dir := "./"
|
||||||
|
|
||||||
|
db, err := scribble.New(dir, nil)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write a fish to the database
|
||||||
|
for _, name := range []string{"onefish", "twofish", "redfish", "bluefish"} {
|
||||||
|
db.Write("fish", name, Fish{Name: name})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read a fish from the database (passing fish by reference)
|
||||||
|
onefish := Fish{}
|
||||||
|
if err := db.Read("fish", "onefish", &onefish); err != nil {
|
||||||
|
fmt.Println("Error", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read all fish from the database, unmarshaling the response.
|
||||||
|
records, err := db.ReadAll("fish")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fishies := []Fish{}
|
||||||
|
for _, f := range records {
|
||||||
|
fishFound := Fish{}
|
||||||
|
if err := json.Unmarshal([]byte(f), &fishFound); err != nil {
|
||||||
|
fmt.Println("Error", err)
|
||||||
|
}
|
||||||
|
fishies = append(fishies, fishFound)
|
||||||
|
}
|
||||||
|
|
||||||
|
// // Delete a fish from the database
|
||||||
|
// if err := db.Delete("fish", "onefish"); err != nil {
|
||||||
|
// fmt.Println("Error", err)
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // Delete all fish from the database
|
||||||
|
// if err := db.Delete("fish", ""); err != nil {
|
||||||
|
// fmt.Println("Error", err)
|
||||||
|
// }
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user