-
Notifications
You must be signed in to change notification settings - Fork 115
/
02-query.go
132 lines (120 loc) · 2.93 KB
/
02-query.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
125
126
127
128
129
130
131
132
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/guonaihong/gout"
"time"
)
// =====================设置查询字符串example==================
// gout使用SetQuery来设置查询字符串
// 其中SetQuery支持多种数据类型 map/struct/string/array
type testQuery struct {
// struct里面的form tag是gin用来绑定数据用的,query才是gout需要的tag
Q1 string `query:"q1" form:"q1"`
Q2 int `query:"q2" form:"q2"`
Q3 float32 `query:"q3" form:"q3"`
Q4 float64 `query:"q4" form:"q4"`
Q5 time.Time `query:"q5" form:"q5" time_format:"unix" time_location:"Asia/Shanghai"`
Q6 time.Time `query:"q6" form:"q6" time_format:"unixNano" time_location:"Asia/Shanghai"`
Q7 time.Time `query:"q7" form:"q7" time_format:"2006-01-02" time_location:"Asia/Shanghai"`
}
func mapExample() {
// 1.使用gout.H
fmt.Printf("======1. SetQuery======use gout.H=====\n")
err := gout.GET(":8080/test.query").
Debug(true).
SetQuery(gout.H{"q1": "v1",
"q2": 2,
"q3": float32(3.14),
"q4": 4.56,
"q5": time.Now().Unix(),
"q6": time.Now().UnixNano(),
"q7": time.Now().Format("2006-01-02")}).
Do()
if err != nil {
fmt.Printf("%s\n", err)
return
}
}
func arrayExample() {
// 2.使用数组变量
fmt.Printf("======2. SetQuery======use array=====\n")
err := gout.GET(":8080/test.query").
Debug(true).
SetQuery(gout.A{"q1", "v1",
"q2", 2,
"q3", float32(3.14),
"q4", 4.56,
"q5", time.Now().Unix(),
"q6", time.Now().UnixNano(),
"q7", time.Now().Format("2006-01-02")}).
Do()
if err != nil {
fmt.Printf("%s\n", err)
return
}
}
func structExample() {
// 3.使用结构体
// 使用结构体需要设置query tag
fmt.Printf("======3. SetQuery======use struct=====\n")
err := gout.GET(":8080/test.query").
Debug(true).
SetQuery(testQuery{Q1: "v1",
Q2: 2,
Q3: float32(3.14),
Q4: 4.56,
Q5: time.Now(),
Q6: time.Now(),
Q7: time.Now()}).
Do()
if err != nil {
fmt.Printf("%s\n", err)
return
}
}
func stringExample() {
// 4.使用string
fmt.Printf("======4. SetQuery======use string=====\n")
err := gout.GET(":8080/test.query").
Debug(true).
SetQuery("q1=v1&q2=2&q3=3.14&q4=3.1415&q5=1564295760&q6=1564295760000001000&q7=2019-07-28").
Do()
if err != nil {
fmt.Printf("%s\n", err)
return
}
}
func bytesExample() {
// 4.使用string
fmt.Printf("======4. SetQuery======use bytes=====\n")
err := gout.GET(":8080/test.query").
Debug(true).
SetQuery([]byte("q1=v1&q2=2&q3=3.14&q4=3.1415&q5=1564295760&q6=1564295760000001000&q7=2019-07-28")).
Do()
if err != nil {
fmt.Printf("%s\n", err)
return
}
}
func main() {
go server()
time.Sleep(time.Millisecond)
mapExample()
structExample()
arrayExample()
stringExample()
bytesExample()
}
func server() {
router := gin.New()
router.GET("/test.query", func(c *gin.Context) {
q2 := testQuery{}
err := c.ShouldBindQuery(&q2)
if err != nil {
c.String(500, "fail")
return
}
})
router.Run()
}