Files
mayfly-go/server/pkg/utils/collx/map.go

45 lines
734 B
Go
Raw Normal View History

package collx
2020-09-01 10:34:11 +08:00
import (
"reflect"
"strconv"
)
func GetString4Map(m map[string]any, key string) string {
2020-09-01 10:34:11 +08:00
return m[key].(string)
}
func GetInt4Map(m map[string]any, key string) int {
2020-09-01 10:34:11 +08:00
i := m[key]
iKind := reflect.TypeOf(i).Kind()
if iKind == reflect.Int {
return i.(int)
}
if iKind == reflect.String {
i, _ := strconv.Atoi(i.(string))
return i
}
return 0
}
2021-04-16 15:10:07 +08:00
// map构造器
type mapBuilder struct {
m map[string]any
2021-04-16 15:10:07 +08:00
}
func MapBuilder(key string, value any) *mapBuilder {
2021-04-16 15:10:07 +08:00
mb := new(mapBuilder)
mb.m = make(map[string]any, 4)
2021-04-16 15:10:07 +08:00
mb.m[key] = value
return mb
}
func (mb *mapBuilder) Put(key string, value any) *mapBuilder {
2021-04-16 15:10:07 +08:00
mb.m[key] = value
return mb
}
func (mb *mapBuilder) ToMap() map[string]any {
2021-04-16 15:10:07 +08:00
return mb.m
}