Wednesday, August 16, 2023

Iterators in GoLang

 Iterators in Go — Bitfield Consulting

for _, v := range Items() {
  ... // do something with v
}

An iterator is just a function, but with a particular signature. For example

func iterateItems(yield func(Item) bool) bool {
  items := []Item{1, 2, 3}
  for _, v := range items {
    if !yield(v) {
        return false
    }
  }
  return true
}



No comments: