-
Notifications
You must be signed in to change notification settings - Fork 9
/
example_test.go
48 lines (38 loc) · 935 Bytes
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package redis_test
import (
"fmt"
"github.com/gosuri/go-store/redis"
)
type Hacker struct {
ID string
Name string
Birthyear int
}
func (h *Hacker) Key() string {
return h.ID
}
func (h *Hacker) SetKey(k string) {
h.ID = k
}
func Example() {
store, err := redis.NewStore("", "")
if err != nil {
panic(err) // handle error
}
// Save a hacker in the store with a auto-generated uuid
if err := store.Write(&Hacker{Name: "Alan Turing", Birthyear: 1912}); err != nil {
panic(err) // handle error
}
var hackers []Hacker
// Populate hackers slice with ids of all hackers in the store
store.List(&hackers)
alan := hackers[0]
store.Read(&alan)
fmt.Println("Hello,", alan.Name)
fmt.Println("Listing all", len(hackers), "hackers")
// Fetches all hackers with names from the store
store.ReadMultiple(hackers)
for _, h := range hackers {
fmt.Printf("%s (%d) (%s)\n", h.Name, h.Birthyear, h.ID)
}
}