-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy path01-color-json.go
124 lines (106 loc) · 2.25 KB
/
01-color-json.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/guonaihong/gout"
"time"
)
// ================SetJSON=接口用法===============
// gout使用SetJSON函数发送json请求至服务端
// 亮点有SetJSON函数支持多种数据类型,map/struct/array/string/bytes
// 下面的xxxExample对应该类型的用法
func mapExample() {
fmt.Printf("\n\n1.=============color json======map example=====\n\n")
err := gout.POST(":8080/colorjson").
Debug(true).
SetJSON(gout.H{"str": "foo",
"num": 100,
"bool": false,
"null": nil,
"array": gout.A{"foo", "bar", "baz"},
"obj": gout.H{"a": 1, "b": 2},
}).Do()
if err != nil {
fmt.Printf("err = %v\n", err)
}
}
func structExample() {
type req struct {
Str string `json:"str"`
Num int `json:"num"`
Bool bool `json:"bool"`
Null *int `json:"null"`
}
fmt.Printf("\n\n2.=============color json======struct example=====\n\n")
err := gout.POST(":8080/colorjson").
Debug(true).
SetJSON(req{Str: "foo",
Num: 100,
Bool: false,
Null: nil,
}).Do()
if err != nil {
fmt.Printf("err = %v\n", err)
}
}
var query = `
{
"query": {
"bool": {
"must": [
{
"exists": {
"field": "voice"
}
},
{
"match": {
"errcode": 3
}
},
{
"range": {
"time": {
"lt": "2020-01-13T23:16:04+08:00",
"gt": "2020-01-13T00:00:00+08:00"
}
}
}
]
}
}
}
`
func stringExample() {
fmt.Printf("\n\n3.=============color json======string example=====\n\n")
err := gout.POST(":8080/colorjson").
Debug(true).
SetJSON(query).Do()
if err != nil {
fmt.Printf("err = %v\n", err)
}
}
func bytesExample() {
fmt.Printf("\n\n4.=============color json======bytes example=====\n\n")
err := gout.POST(":8080/colorjson").
Debug(true).
SetJSON(query).Do()
if err != nil {
fmt.Printf("err = %v\n", err)
}
}
func main() {
go server()
time.Sleep(time.Millisecond * 200)
mapExample()
structExample()
stringExample()
bytesExample()
}
func server() {
router := gin.New()
router.POST("/colorjson", func(c *gin.Context) {
c.JSON(200, gin.H{"str2": "str2 val", "int2": 2})
})
router.Run()
}