collections: Use Go 1.23 iterator patterns instead of exposing internals
In the first round of collections we exposed the internals of Set and Map as a pragmatic concession to allow efficiently iterating over the elements.
Go 1.23 is planned to stabilize "range-over-func" (https://github.com/golang/go/issues/61405) and package iter (https://github.com/golang/go/issues/61897) which together allow us to expose an efficient iteration API without exposing the internal representations of these collections.
The benefit for Set is pretty marginal, but the benefit for Map is more substantial: it avoids the oddity of exposing the MapElem values directly and instead returns the key and value directly so that usage is very similar to iterating over a built-in map.
I tried this now because I wanted to confirm that we could indeed retrofit these collection types with an iterator without too much disruption, and that seems to be true. We aren't using Go 1.23 yet and so we can't merge this, but I tested it using Go 1.22 and GOEXPERIMENT=rangefunc and it seems to work just fine. I expect we will be able to merge a PR like this shortly after adopting Go 1.23, which isn't yet released at the time I'm writing this but is likely to arrive in August.
I'm no longer at HashiCorp after today, so I won't be able to work on this any further but I'm going to leave it open to keep it visible in case someone else wants to take over this branch in a new PR.
I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions. If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.