-
Notifications
You must be signed in to change notification settings - Fork 115
/
03a-set-header.go
102 lines (93 loc) · 2.03 KB
/
03a-set-header.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
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/guonaihong/gout"
"time"
)
// ============== gout 设置http header example============
// 使用SetHeader接口 设置http header
// SetHeader支持的数据类型有map/array/struct
type testHeader struct {
H1 string `header:"h1"`
H2 int `header:"h2"`
H3 float32 `header:"h3"`
H4 float64 `header:"h4"`
H5 time.Time `header:"h5" time_format:"unix"`
H6 time.Time `header:"h6" time_format:"unixNano"`
H7 time.Time `header:"h7" time_format:"2006-01-02"`
}
func mapExample() {
// 1.使用gout.H
fmt.Printf("======1. SetHeader======use gout.H=====\n")
err := gout.GET(":8080/test.header").
Debug(true).
SetHeader(gout.H{"h1": "v1",
"h2": 2,
"h3": float32(3.14),
"h4": 4.56,
"h5": time.Now().Unix(),
"h6": time.Now().UnixNano(),
"h7": time.Now().Format("2006-01-02")}).
Do()
if err != nil {
fmt.Printf("%s\n", err)
return
}
}
func arrayExample() {
// 2.使用数组变量
fmt.Printf("======2. SetHeader======use array=====\n")
err := gout.GET(":8080/test.header").
Debug(true).
SetHeader(gout.A{"h1", "v1",
"h2", 2,
"h3", float32(3.14),
"h4", 4.56,
"h5", time.Now().Unix(),
"h6", time.Now().UnixNano(),
"h7", time.Now().Format("2006-01-02")}).
Do()
if err != nil {
fmt.Printf("%s\n", err)
return
}
}
func structExample() {
// 3.使用结构体
// 使用结构体需要设置"header" tag
fmt.Printf("======3. SetHeader======use struct=====\n")
err := gout.GET(":8080/test.header").
Debug(true).
SetHeader(testHeader{H1: "v1",
H2: 2,
H3: float32(3.14),
H4: 4.56,
H5: time.Now(),
H6: time.Now(),
H7: time.Now()}).
Do()
if err != nil {
fmt.Printf("%s\n", err)
return
}
}
func main() {
go server()
time.Sleep(time.Millisecond)
mapExample()
arrayExample()
structExample()
}
func server() {
router := gin.New()
router.GET("/test.header", func(c *gin.Context) {
h2 := testHeader{}
err := c.BindHeader(&h2)
if err != nil {
c.String(500, "fail")
return
}
})
router.Run()
}