迭代戈兰地图的所有键 [英] Iterating over all the keys of a Golang map
本文介绍了迭代戈兰地图的所有键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
的元素由
len()
给出,但如果我有一个地图,如: m:= map [string] string {key1:val1,key2:val2};
如何迭代所有的键?
解决方案
https://play.golang.org / p / JGZ7mN0-U-
for k,v:= range m {
fmt。 Printf(key [%s] value [%s] \\\
,k,v)
}
或
for k:= range m {
fmt.Printf(key [% s] value [%s] \\\
,k,m [k])
}
为语言规范语句
指定第一个值是键,第二个变量是值,但不一定存在。
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:
m := map[string]string{ "key1":"val1", "key2":"val2" };
how do I iterate over all the keys?
解决方案
https://play.golang.org/p/JGZ7mN0-U-
for k, v := range m {
fmt.Printf("key[%s] value[%s]\n", k, v)
}
or
for k := range m {
fmt.Printf("key[%s] value[%s]\n", k, m[k])
}
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.
这篇关于迭代戈兰地图的所有键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文