golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
120.96k stars 17.36k forks source link

proposal: archive/tar: add iterator form of (&Reader).Next() #68062

Open sivchari opened 1 week ago

sivchari commented 1 week ago

Proposal Details

In Go 1.23, we can use range-over-func a.k.a iterator and developers propose to apply for the package into some packages.

For instance, the following issues are easy to know what kind of content are proposed. https://github.com/golang/go/issues/61897 https://github.com/golang/go/issues/64341

This proposal is similar with above it. I think it's good to apply for archive/tar, too.

func (tr *Reader) NextIter() iter.Seq2[*Header, error] {
    return func(yield func(*Header, error) bool) {
        hdr, err := tr.Next()
        if err == io.EOF {
            return
        }
        if !yield(hdr, err) {
            return
        }
    }
}

Thanks.

ianlancetaylor commented 1 week ago

Perhaps AllHeaders would be a better name.

Also I think error handling for this sort of iterator is currently an open issue. We may want to wait a bit to see if we come to a consensus on iterators where each value can fail.

robpike commented 1 week ago

I think that the question of adding iterators to existing packages is an example of what I was talking about in https://github.com/golang/go/issues/67510