3 ms·
If the underlying data source changes then that is something happening on my side of the contract. But io.Reader explicitly deals with populating a caller supp
by XorNot 1y ago
If the underlying data source changes then that is something happening on my side of the contract.
But io.Reader explicitly deals with populating a caller supplied buffer with whatever the next valid set of bytes is.
The caller thus knows the buffer won't change while the handle it.
- jchw 1y agoNot 100% sure what is meant here. io.Reader itself only exposes the ability to do read-only access, but if you upcast you are not limited to only io.Reader's functionality. Now of course if you had a function that upcasted e.g. io.Reader to io.Writer and wrote to the provided stream, that would be weird. On the other hand, there is no such issue with merely grabbing an interface { Bytes() []byte } out of an io.Reader, calling the Bytes() method, and then not modifying the returned buffer. I don't think that introduces any hazards or contract violations and as best as I can tell is idiomatic Go and not discouraged.