遍历映射的所有键

huangapple go评论137阅读模式
英文:

Iterating over all the keys of a map

问题

有没有办法获取Go语言地图中所有键的列表?元素的数量可以通过len()函数获得,但如果我有一个地图,像这样:

  1. m := map[string]string{ "key1":"val1", "key2":"val2" };

我如何遍历所有的键?

英文:

Is there a way to get a list of all the keys in a Go language map? The number of elements is given by len(), but if I have a map like:

  1. m := map[string]string{ "key1":"val1", "key2":"val2" };

How do I iterate over all the keys?

答案1

得分: 734

https://play.golang.org/p/JGZ7mN0-U-

  1. for k, v := range m {
  2. fmt.Printf("key[%s] value[%s]\n", k, v)
  3. }

或者

  1. for k := range m {
  2. fmt.Printf("key[%s] value[%s]\n", k, m[k])
  3. }

Go语言for语句的规范指定第一个值是键,第二个变量是值,但不一定要存在。

英文:

https://play.golang.org/p/JGZ7mN0-U-

  1. for k, v := range m {
  2. fmt.Printf("key[%s] value[%s]\n", k, v)
  3. }

or

  1. for k := range m {
  2. fmt.Printf("key[%s] value[%s]\n", k, m[k])
  3. }

Go language specs for for statements specifies that the first value is the key, the second variable is the value, but doesn't have to be present.

答案2

得分: 21

这是一种获取地图键的简单方法。

  1. // 返回给定地图的键
  2. func Keys(m map[string]interface{}) (keys []string) {
  3. for k := range m {
  4. keys = append(keys, k)
  5. }
  6. return keys
  7. }
  8. // 使用 `Keys` 函数
  9. func main() {
  10. m := map[string]interface{}{
  11. "foo": 1,
  12. "bar": true,
  13. "baz": "baz",
  14. }
  15. fmt.Println(Keys(m)) // [foo bar baz]
  16. }
英文:

Here's some easy way to get slice of the map-keys.

  1. // Return keys of the given map
  2. func Keys(m map[string]interface{}) (keys []string) {
  3. for k := range m {
  4. keys = append(keys, k)
  5. }
  6. return keys
  7. }
  8. // use `Keys` func
  9. func main() {
  10. m := map[string]interface{}{
  11. "foo": 1,
  12. "bar": true,
  13. "baz": "baz",
  14. }
  15. fmt.Println(Keys(m)) // [foo bar baz]
  16. }

答案3

得分: 19

有没有办法获取Go语言中map的所有键的列表?

  1. ks := reflect.ValueOf(m).MapKeys()

如何遍历所有的键?

使用接受的答案:

  1. for _, k := range m { ... }
英文:

> Is there a way to get a list of all the keys in a Go language map?

  1. ks := reflect.ValueOf(m).MapKeys()

> how do I iterate over all the keys?

Use the accepted answer:

  1. for _, k := range m { ... }

答案4

得分: 6

一个类型不可知的解决方案:

  1. for _, key := range reflect.ValueOf(yourMap).MapKeys() {
  2. value := yourMap.MapIndex(key).Interface()
  3. fmt.Println("键:", key, "值:", value)
  4. }
英文:

A Type agnostic solution:

  1. for _, key := range reflect.ValueOf(yourMap).MapKeys() {
  2. value := yourMap.MapIndex(key).Interface()
  3. fmt.Println("Key:", key, "Value:", value)
  4. }

答案5

得分: 4

使用泛型:

  1. func Keys[K comparable, V any](m map[K]V) []K {
  2. keys := make([]K, 0, len(m))
  3. for k := range m {
  4. keys = append(keys, k)
  5. }
  6. return keys
  7. }
英文:

Using Generics:

  1. func Keys[K comparable, V any](m map[K]V) []K {
  2. keys := make([]K, 0, len(m))
  3. for k := range m {
  4. keys = append(keys, k)
  5. }
  6. return keys
  7. }

答案6

得分: 0

对于map[string]string的排序键

  1. package main
  2. import (
  3. "fmt"
  4. "sort"
  5. )
  6. func main() {
  7. m := map[string]string{"key1": "val1", "key2": "val2"}
  8. sortStringMap(m)
  9. }
  10. // sortStringMap按键排序打印[string]string
  11. func sortStringMap(m map[string]string) {
  12. var keys []string
  13. for key := range m {
  14. keys = append(keys, key)
  15. }
  16. sort.Strings(keys) // 对键进行排序
  17. for _, key := range keys {
  18. fmt.Printf("%s\t:%s\n", key, m[key])
  19. }
  20. }

输出:

  1. key1 :val1
  2. key2 :val2
英文:

For sorted keys of map[string]string.

  1. package main
  2. import (
  3. "fmt"
  4. "sort"
  5. )
  6. func main() {
  7. m := map[string]string{"key1": "val1", "key2": "val2"}
  8. sortStringMap(m)
  9. }
  10. // sortStringMap prints the [string]string as keys sorted
  11. func sortStringMap(m map[string]string) {
  12. var keys []string
  13. for key := range m {
  14. keys = append(keys, key)
  15. }
  16. sort.Strings(keys) // sort the keys
  17. for _, key := range keys {
  18. fmt.Printf("%s\t:%s\n", key, m[key])
  19. }
  20. }

output:

  1. key1 :val1
  2. key2 :val2

huangapple
  • 本文由 发表于 2009年12月4日 01:15:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/1841443.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定