forked from TruthHun/DocHub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conv.go
41 lines (36 loc) · 881 Bytes
/
conv.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
package conv
import (
"encoding/json"
"fmt"
"strconv"
"strings"
)
//将interface数据转json
func InterfaceToJson(itf interface{}) (string, error) {
b, err := json.Marshal(&itf)
return string(b), err
}
//interface转整型
func InterfaceToInt(itf interface{}) (num int, err error) {
return strconv.Atoi(fmt.Sprintf("%v", itf))
}
//首字母大写
func UpperFirst(str string) string {
return strings.Replace(str, str[0:1], strings.ToUpper(str[0:1]), 1)
}
//把url路径中的path请求,变成key val,path参数值形式:/user/list/p/1,转成map[string]string
func Path2Map(path string) map[string]string {
var data = make(map[string]string)
slice := strings.Split(strings.Trim(path, "/"), "/")
cnt := len(slice)
if cnt%2 == 1 {
cnt = cnt - 1
}
if cnt > 0 {
for i := 0; i < cnt; {
data[slice[i]] = slice[(i + 1)]
i = i + 2
}
}
return data
}