patterngoCritical
Iterating over all the keys of a map
Viewed 0 times
keysiteratingtheoverallmap
Problem
Is there a way to get a list of all the keys in a Go language map? The number of elements is given by
How do I iterate over all the keys?
len(), but if I have a map like:m := map[string]string{ "key1":"val1", "key2":"val2" };How do I iterate over all the keys?
Solution
https://play.golang.org/p/JGZ7mN0-U-
or
Go language specs for
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.Code Snippets
for k, v := range m {
fmt.Printf("key[%s] value[%s]\n", k, v)
}for k := range m {
fmt.Printf("key[%s] value[%s]\n", k, m[k])
}Context
Stack Overflow Q#1841443, score: 779
Revisions (0)
No revisions yet.