Iterate over two maps at once

2,192 views
Skip to first unread message

Tibi

unread,
Jul 4, 2011, 3:03:53 PM7/4/11
to golan...@googlegroups.com
Hello,

I would like to iterate at once over two maps that have common keys but they also have different keys. I only care about the common keys.
In C++, I would do this with iterators, but in Go I can only iterate with range. The problem is that I have to choose which map to iterate over.

Any clues?

Tibor

jimmy frasche

unread,
Jul 4, 2011, 3:07:05 PM7/4/11
to golan...@googlegroups.com
If you only care about the common keys why not just arbitrarily pick
one map and in each iteration continue if the key is not in the other
map like

var a, b map[something]something
....
for k, v := range a {
if v2, ok := b[k]; ok {
//code

Reply all
Reply to author
Forward
0 new messages