-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy path13-form-data.go
106 lines (90 loc) · 2.02 KB
/
13-form-data.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
package main
import (
"fmt"
"time"
"github.com/gin-gonic/gin"
"github.com/guonaihong/gout"
)
type testForm struct {
Mode string `form:"mode"`
Text string `form:"text"`
//Voice []byte `form:"voice" form-mem:"true"` //todo open
}
type testForm2 struct {
Mode string `form:"mode"`
Text string `form:"text"`
Voice string `form:"voice" form-file:"file"` //从文件中读取
Voice2 []byte `form:"voice2" form-file:"mem"` //从内存中构造
}
// 使用map装载数据
func mapExample() {
// 1.使用gout.H
fmt.Printf("\n\n====1. use gout.H==============\n\n")
code := 0
err := gout.
POST(":8080/test.form").
Debug(true).
SetForm(gout.H{"mode": "A",
"text": "good",
"voice": gout.FormFile("../testdata/voice.pcm"),
"voice2": gout.FormMem("pcm")}).
Code(&code).
Do()
if err != nil || code != 200 {
fmt.Printf("%s:code = %d\n", err, code)
return
}
}
// 使用结构体装载数据
func structExample() {
code := 0
// 2.使用结构体里面的数据
fmt.Printf("\n\n====2. use struct==============\n\n")
err := gout.
POST(":8080/test.form").
Debug(true).
SetForm(testForm2{
Mode: "A",
Text: "good",
Voice: "../testdata/voice.pcm",
Voice2: []byte("pcm")}).
Code(&code).Do()
if err != nil || code != 200 {
}
}
// 自定义filename
func mapExample2() {
code := 0
// 2.使用结构体里面的数据
fmt.Printf("\n\n====3. use struct==============\n\n")
err := gout.
POST(":8080/test.form").
Debug(true).
SetForm(gout.H{
"Mode": "A",
"Text": "good",
"Voice": gout.FormType{FileName: "test-file-name", File: gout.FormFile("../testdata/voice.pcm")},
}).
Code(&code).Do()
if err != nil || code != 200 {
}
}
func main() {
go server()
time.Sleep(time.Millisecond * 500) //sleep下等服务端真正起好
mapExample()
structExample()
mapExample2()
}
func server() {
router := gin.New()
router.POST("/test.form", func(c *gin.Context) {
t2 := testForm{}
err := c.Bind(&t2)
if err != nil {
fmt.Printf("err = %s\n", err)
return
}
})
router.Run()
}