/
structToURL.go
47 lines (38 loc) · 1.07 KB
/
structToURL.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
package httpUtil
// ------------ format struct to url form -----------------------------------
func StructToUrlValues(st interface{}) url.Values {
s := reflect.ValueOf(st)
typeOf := s.Type()
// get length of struct
length := s.NumField()
// create map of url.Values
m := make(map[string][]string)
// loop over struct
for i := 0; i < length; i++ {
var value string
val := s.Field(i)
// check struct value type and convert it to string
switch s.Field(i).Kind() {
case reflect.Int:
value = strconv.FormatInt(val.Int(), 10)
case reflect.Float64:
value = strconv.FormatFloat(val.Float(), 'f', 2, 64)
case reflect.Float32:
value = strconv.FormatFloat(val.Float(), 'f', 2, 32)
case reflect.Bool:
value = strconv.FormatBool(val.Bool())
case reflect.String:
value = val.String()
}
// get struct Tag
field := typeOf.Field(i).Tag.Get("urlVal")
// update map
if field == "" {
m[typeOf.Field(i).Name] = append(m[field], value)
} else {
m[field] = append(m[typeOf.Field(i).Name], value)
}
}
// return struct converted to map
return m
}