Skip to content

feat: Official Iterator support for Go1.23+ & minor fixes#34

Open
leonardodalinky wants to merge 2 commits intoliyue201:masterfrom
leonardodalinky:iter_go1.23
Open

feat: Official Iterator support for Go1.23+ & minor fixes#34
leonardodalinky wants to merge 2 commits intoliyue201:masterfrom
leonardodalinky:iter_go1.23

Conversation

@leonardodalinky
Copy link

I made official supports of iter package in Go standard for #33. Now we can try the range-based for-loops to iterate some of the types!

I use //go:build go1.23 build tag for these files with new features to avoid breaking changes, like the code written in previous Go version (<1.23). I think this will not do harm to the users using older Go compilers.

All the types with naive Iterators are implemented with new Iter(), Iter2(), IterMut() and IterMut2():

  • array
  • deque
  • bidlist
  • simplelist
  • treemap: exclude IterMut()
  • rbtree: exclude IterMut()
  • set: only Iter()
  • slice
  • vector

Example for array:

a := array.New[int](10)

// Iter()
for v := range a.Iter() {
    fmt.Println(v)
}

// IterMut()
for vPtr := range a.IterMut() {
    *vPtr *= 2
}

// Iter2()
for i, v := range a.Iter2() {
    fmt.Println(i, v)
}

// IterMut2()
for i, vPtr := range a.IterMut2() {
    *vPtr = i
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant