Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

io: ReadAtLeast: example does not produce the suggested error condition #36245

Closed
zacsketches opened this issue Dec 21, 2019 · 1 comment
Closed

Comments

@zacsketches
Copy link

zacsketches commented Dec 21, 2019

I was using io.ReadAtLeast today to implement serial reads for some industrial control devices
and was reading the documentation when I noticed that the function definition for
io.ReadAtLeast was not aligned well with the example.

ReadAtLeast reads from r into buf until it has read at least min bytes. It returns the number of bytes copied and an error if fewer bytes were read. The error is EOF only if no bytes were read. If an EOF happens after reading fewer than min bytes, ReadAtLeast returns ErrUnexpectedEOF. If min is greater than the length of buf, ReadAtLeast returns ErrShortBuffer. On return, n >= min if and only if err == nil. If r returns an error having read at least min bytes, the error is dropped.

Focusing on the error condition for ErrUnexpectedEOF, this section of the example program

       // minimal read size bigger than io.Reader stream
	longBuf := make([]byte, 64)
	if _, err := io.ReadAtLeast(r, longBuf, 64); err != nil {
		fmt.Println("error:", err)
	}

is designed to demonstrate this error. However, r, the argument to io.ReadAtLeast in this part of the code, has already been used. So instead of this section of the example generating ErrUnexpectedEOF it just generates EOF.

I suggest that the example for ReadAtLeast be changed to the code at this playground example. https://play.golang.org/p/nWx28cfHB-G

Better still might be an example that demonstrates EOF, ErrUnexpectedEOF, and ErrShortbuffer. Happy to submit a PR to make the docs and examples more illustrative for the full range of error reporting!

@gopherbot
Copy link

Change https://golang.org/cl/212404 mentions this issue: io: show ErrUnexpectedEOF in ExampleReadAtLeast

@golang golang locked and limited conversation to collaborators Dec 26, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants