模块

github地址 : https://github.com/nitishm/go-rejson

json

在这里插入图片描述

代码

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main

import (
"encoding/json"
"fmt"
"log"

goredis "github.com/go-redis/redis"

"github.com/gomodule/redigo/redis"
"github.com/nitishm/go-rejson"
)

func main() {
// Redigo Client "github.com/gomodule/redigo/redis"
rh := rejson.NewReJSONHandler()
// connect redis
conn, err := redis.Dial("tcp", "192.168.1.131:6379")
if err != nil {
return
}
// 绑定
rh.SetRedigoClient(conn)
js1, err := redis.Bytes(rh.JSONGet("group", "."))
if err != nil {
log.Fatalf("Failed to JSONGet")
return
}
// group
var group interface{}
err = json.Unmarshal(js1, &group)
if err != nil {
return
}
fmt.Println("group1")
fmt.Println(group)
// _1
js1, err = redis.Bytes(rh.JSONGet("group", "_1"))
if err != nil {
log.Fatalf("Failed to JSONGet")
return
}
var _1 interface{}
err = json.Unmarshal(js1, &_1)
if err != nil {
return
}
fmt.Println("_1")
fmt.Println(_1)

err = conn.Close()
if err != nil {
return
}

// GoRedis Client "github.com/go-redis/redis"
// connect redis
cli := goredis.NewClient(&goredis.Options{Addr: "192.168.1.131:6379", // use default Addr
Password: "", // no password set
DB: 0, // use default DB
})
// 绑定
rh.SetGoRedisClient(cli)
js2, err := redis.Bytes(rh.JSONGet("group", "."))
if err != nil {
log.Fatalf("Failed to JSONGet")
return
}
//
var group2 interface{}
err = json.Unmarshal(js2, &group2)
if err != nil {
return
}
fmt.Println("group2")
fmt.Println(group2)
err = cli.Close()
if err != nil {
return
}
return
}