-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathghttp_func.go
55 lines (50 loc) · 1.31 KB
/
ghttp_func.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
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package ghttp
import (
"strings"
"github.com/gogf/gf/encoding/gurl"
"github.com/gogf/gf/util/gconv"
)
// 构建请求参数,参数支持任意数据类型,常见参数类型为string/map。
// 如果参数为map类型,参数值将会进行urlencode编码;可以通过 noUrlEncode:true 参数取消编码。
func BuildParams(params interface{}, noUrlEncode ...bool) (encodedParamStr string) {
m, urlEncode := gconv.Map(params), true
if len(m) == 0 {
return gconv.String(params)
}
if len(noUrlEncode) == 1 {
urlEncode = !noUrlEncode[0]
}
s := ""
for k, v := range m {
if len(encodedParamStr) > 0 {
encodedParamStr += "&"
}
s = gconv.String(v)
if urlEncode && len(s) > 6 && strings.Compare(s[0:6], "@file:") != 0 {
s = gurl.Encode(s)
}
encodedParamStr += k + "=" + s
}
return
}
// 友好地调用方法
func niceCallFunc(f func()) {
defer func() {
if err := recover(); err != nil {
switch err {
case gEXCEPTION_EXIT:
fallthrough
case gEXCEPTION_EXIT_ALL:
return
default:
panic(err)
}
}
}()
f()
}