Files
mayfly-go/server/pkg/utils/collx/map.go
meilin.huang d300f604f1 review
2023-10-12 12:14:56 +08:00

25 lines
482 B
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package collx
import "mayfly-go/pkg/utils/stringx"
// M is a shortcut for map[string]any
type M map[string]any
// 将偶数个元素转为对应的M (map[string]any)
//
// 偶数索引为key奇数为value
func Kvs(elements ...any) M {
myMap := make(map[string]any)
for i := 0; i < len(elements); i += 2 {
key := stringx.AnyToStr(elements[i])
if i+1 < len(elements) {
value := elements[i+1]
myMap[key] = value
} else {
myMap[key] = nil
}
}
return myMap
}