Files
mayfly-go/server/pkg/utils/collx/map.go
2023-10-20 21:31:46 +08:00

25 lines
476 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/anyx"
// 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 := anyx.ToString(elements[i])
if i+1 < len(elements) {
value := elements[i+1]
myMap[key] = value
} else {
myMap[key] = nil
}
}
return myMap
}