Skip to content

Add Go 1.23 iterators to collection types#10

Merged
stringintech merged 6 commits intomainfrom
go-iter
Nov 23, 2025
Merged

Add Go 1.23 iterators to collection types#10
stringintech merged 6 commits intomainfrom
go-iter

Conversation

@stringintech
Copy link
Owner

This PR adds idiomatic Go 1.23 iterators (iter.Seq) to collection types throughout the library. Each type with indexed access now provides three iterator methods following a consistent pattern:

  • Items() - iterate over all items
  • ItemsRange(from, to) - iterate over a specific range [from, to)
  • ItemsFrom(from) - iterate from a starting index to the end

The iterators are added to: Block (transactions), Chain (block tree entries), Transaction (inputs and outputs), BlockSpentOutputs (transaction spent outputs), and TransactionSpentOutputs (coins).

Example: Iterating over block transactions

Previous approach:

for i := uint64(0); i < block.CountTransactions(); i++ {
    tx, err := block.GetTransactionAt(i)
    if err != nil {
        return err
    }
    // process tx
}

New approach:

for tx := range block.Transactions() {
    // process tx
}

Example: Iterating over chain entries from a specific height

Previous approach:

for h := startHeight; h <= chain.GetHeight(); h++ {
    entry := chain.GetByHeight(h)
    if entry == nil {
        break
    }
    // process entry
}

New approach:

for entry := range chain.EntriesFrom(startHeight) {
    // process entry
}

Also adds test for iterators and groups block tests into subtests.
Also adds test for iterators and groups BlockSpentOutputs tests into subtests.
Also adds test for iterators and groups chain tests into subtests.
Also adds test for iterators and groups Transaction tests into subtests.
Also adds test for iterators.
@stringintech stringintech merged commit 8a3da24 into main Nov 23, 2025
8 checks passed
@stringintech stringintech deleted the go-iter branch November 23, 2025 09:30
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