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.
25 lines
509 B
25 lines
509 B
package utils
|
|
|
|
import "math/rand"
|
|
|
|
func RandMapValue(m map[string]string) string {
|
|
return m[RandMapKey(m)]
|
|
}
|
|
func RandMapKey(m map[string]string) string {
|
|
mapKeys := make([]string, 0, len(m)) // pre-allocate exact size
|
|
for key := range m {
|
|
mapKeys = append(mapKeys, key)
|
|
}
|
|
return mapKeys[rand.Intn(len(mapKeys))]
|
|
}
|
|
|
|
// MapKey 知道值 拿第一个key
|
|
func MapKey(m map[string]string, value string) (string, bool) {
|
|
for k, v := range m {
|
|
if v == value {
|
|
return k, true
|
|
}
|
|
}
|
|
return "", false
|
|
}
|