title: How can I create a first-class map iterator in Go?
body: <p>I am writing a function that iterates over the entries in a map.  I want to be able to deal cleanly with items which are added or deleted from the map while iterating, like <code>for k, v := range myMap { //...</code> does, but I am processing just one key/value pair per iteration so I can't use range.  I want something like:</p>
  
  <pre><code>func processItem(i iterator) bool {
       k, v, ok := i.next()
       if(!ok) {
           return false
       }
       process(v)
       return true
  }
  
  var m = make(map[string]widget)
  // ...
  i := makeIterator(m)
  for processItem(i) {
      // code which might add/remove item from m here
  }
  </code></pre>
  
  <p>I know that range is using a '<code>hiter</code>' struct and associated functions, as defined in <code>src/runtime/hashmap.go</code>, to perform iteration.  Is there some way to gain access to this iterator as a reified (first-class) Go object?</p>
  
  <p>Is there an alternative strategy for iterating over a map which would deal well with insertions/deletions but give a first-class iterator object?</p>
  
  <p><strong>Bonus question:</strong> is there an alternative strategy for iterating over a map which could <em>also</em> deal with the map and iterator being serialised to disk and then restored, with iteration continuing from where it left off?  (Obviously the built-in <code>range</code> iterator does not have this capability!)</p>