-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple.go
62 lines (51 loc) · 1.04 KB
/
simple.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"encoding/json"
"fmt"
"github.com/go-redis/redis"
)
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
}
func main() {
client := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "",
DB: 0,
})
pong, err := client.Ping().Result()
fmt.Println(pong, err)
// store basic value
err = client.Set("name", "john", 0).Err()
if err != nil {
fmt.Println(err)
}
// get the value from key string
val, err := client.Get("name").Result()
if err != nil {
fmt.Println(err)
}
fmt.Println(val)
// store composite value
jsonValue, err := json.Marshal(Person{"Devid P.", 12})
if err != nil {
fmt.Println(err)
}
err = client.Set("user111", jsonValue, 0).Err()
if err != nil {
fmt.Println("store err:", err)
}
// get the composite value
comVal, err := client.Get("user111").Bytes()
if err != nil {
fmt.Println(err)
}
fmt.Printf("%s\n", comVal)
var p Person
err = json.Unmarshal(comVal, &p)
if err != nil {
fmt.Println(err)
}
fmt.Printf("%+v\n", p)
}