Most common idiomatic go way of accepting an enumerator to a data source in a public API

In C# the IEnumerator interface would be used which would allow a caller to provide anything from an array, list or more exotic source of data (e.g. the contents of a TCP connection).
I have seen the io.Reader interface and I was hoping that someone could say if accepting an io.Reader is the most idiomatic golang way to accept data from as many data sources as possible with the minimum of fuss for the API users.

Clearly this interface is at the byte level and appropriate restrictions / conversion functions would be needed for non-byte data (e.g. uint32’s) if a byte-wise interface was used.

Alternatives that I have found are to create a potentially unique enumerator type of the form of one of the possibilities from the go cookbook [1] or to use an existing package such as go-enumerate [2] but I get the impression that is not particularly popular. Another choice would be to go for io.ReadSeeker as that would allow the read position to move on only if the object generation was successful, but this may restrict the caller design freedom.

The use case I have in mind is for a public API function that would read data from the source to generate an object it would return by consuming some of the data from the source and leave excess data unread for the next call of the public API.

If anyone has some insight into the most common choice for such an input data source argument type then I would be delighted if you could share it with me.

[1] https://github.com/kjk/go-cookbook/tree/master/3-ways-to-iterate
[2] https://github.com/swdyh/go-enumerable

I note that that the bufio [1] allows byte slices to be read via either io.Reader or buffio.Reader (which allows peeking) and the bytes package [2] provides a richer io.Reader/io.ByteReader/io.Writer Buffer type. The math package [3] contains conversions from floats to/from integer types and the binary package [4] uses io.ByteReader and io.Writer to work with streams.

There is clearly more possibilities to support than I had hoped for.

[1] https://golang.org/pkg/bufio/
[2] https://golang.org/pkg/bytes/

Separate due to forum restrictions:

[3] https://golang.org/pkg/math/
[4] https://golang.org/pkg/encoding/binary/

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.