You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
gorequest/params.go

69 lines
1.2 KiB

2 years ago
package gorequest
import (
"encoding/json"
2 years ago
"go.dtapp.net/gostring"
2 years ago
"log"
)
2 years ago
// Params 参数
2 years ago
type Params map[string]interface{}
2 years ago
// NewParams 新建参数
2 years ago
func NewParams() Params {
P := make(Params)
return P
}
2 years ago
// NewParamsWith 参数使用
2 years ago
func NewParamsWith(params ...Params) Params {
p := make(Params)
for _, v := range params {
p.SetParams(v)
}
return p
}
2 years ago
// Set 设置参数
2 years ago
func (p Params) Set(key string, value interface{}) {
p[key] = value
}
2 years ago
// SetParams 批量设置参数
2 years ago
func (p Params) SetParams(params Params) {
for key, value := range params {
p[key] = value
}
}
2 years ago
// GetParamsString 获取参数字符串
2 years ago
func GetParamsString(src interface{}) string {
switch src.(type) {
case string:
return src.(string)
case int, int8, int32, int64:
case uint8, uint16, uint32, uint64:
case float32, float64:
2 years ago
return gostring.ToString(src)
2 years ago
}
data, err := json.Marshal(src)
if err != nil {
log.Fatal(err)
}
return string(data)
}
2 years ago
// DeepCopy 深度复制
func (p *Params) DeepCopy() map[string]interface{} {
targetMap := make(map[string]interface{})
// 从原始复制到目标
for key, value := range *p {
targetMap[key] = value
}
// 重新申请一个新的map
*p = map[string]interface{}{}
return targetMap
}