u
This commit is contained in:
1 parent
098a061308
commit
994321d049
40 files changed
+1401
-359
No files matched your search
@@ -13,6 +13,10 @@ func New() M {
|
||||
return M{}
|
||||
}
|
||||
|
||||
func NewArr() {
|
||||
//return make([]M, 0, len())
|
||||
}
|
||||
|
||||
// Set 设置单个键值,支持链式调用
|
||||
func (m M) Set(key string, value interface{}) M {
|
||||
m[key] = value
|
||||
@@ -78,33 +82,68 @@ func (m M) GetString(key string) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// GetInt 获取整数类型
|
||||
func (m M) GetInt(key string) int {
|
||||
if v, ok := m[key]; ok {
|
||||
// 核心获取整数的方法,返回 int64
|
||||
func (m M) getIntValue(key string) int64 {
|
||||
if v, ok := m[key]; ok && v != nil {
|
||||
switch val := v.(type) {
|
||||
case int:
|
||||
return val
|
||||
return int64(val)
|
||||
case int8:
|
||||
return int(val)
|
||||
return int64(val)
|
||||
case int16:
|
||||
return int(val)
|
||||
return int64(val)
|
||||
case int32:
|
||||
return int(val)
|
||||
return int64(val)
|
||||
case int64:
|
||||
return int(val)
|
||||
return val
|
||||
case uint:
|
||||
return int64(val)
|
||||
case uint8:
|
||||
return int64(val)
|
||||
case uint16:
|
||||
return int64(val)
|
||||
case uint32:
|
||||
return int64(val)
|
||||
case uint64:
|
||||
return int64(val) // 注意溢出
|
||||
case float32:
|
||||
return int(val)
|
||||
return int64(val)
|
||||
case float64:
|
||||
return int(val)
|
||||
return int64(val)
|
||||
case string:
|
||||
if i, err := strconv.ParseInt(val, 10, 64); err == nil {
|
||||
return int(i)
|
||||
return i
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// GetInt 返回默认 int 类型
|
||||
func (m M) GetInt(key string) int {
|
||||
return int(m.getIntValue(key))
|
||||
}
|
||||
|
||||
// GetInt64 返回 int64
|
||||
func (m M) GetInt64(key string) int64 {
|
||||
return m.getIntValue(key)
|
||||
}
|
||||
|
||||
// GetInt32 返回 int32
|
||||
func (m M) GetInt32(key string) int32 {
|
||||
return int32(m.getIntValue(key))
|
||||
}
|
||||
|
||||
// GetInt16 返回 int16
|
||||
func (m M) GetInt16(key string) int16 {
|
||||
return int16(m.getIntValue(key))
|
||||
}
|
||||
|
||||
// GetInt8 返回 int8
|
||||
func (m M) GetInt8(key string) int8 {
|
||||
return int8(m.getIntValue(key))
|
||||
}
|
||||
|
||||
// Merge 合并另一个 M,后者会覆盖前者同名字段
|
||||
func (m M) Merge(other M) M {
|
||||
for k, v := range other {
|
||||
|
||||
Reference in new issue
Block a user