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

bufio: reading lines is too cumbersome #4802

Closed
bradfitz opened this issue Feb 13, 2013 · 5 comments
Closed

bufio: reading lines is too cumbersome #4802

bradfitz opened this issue Feb 13, 2013 · 5 comments
Milestone

Comments

@bradfitz
Copy link
Contributor

Reading lines from a file is too cumbersome in Go.

People are often drawn to bufio.Reader.ReadLine because of its name, but it has a weird
signature, returning (line []byte, isPrefix bool, err error), and requires a lot of work.

ReadSlice and ReadString require a delimiter byte, which is almost always the obvious
and unsightly '\n', and also can return both a line and an EOF:
http://play.golang.org/p/LSl2Aketpg

This bug is about designing a nicer API for bufio to do the common task of looping over
lines from an io.Reader.

Things to think about:

-- functional or not, like my earlier https://golang.org/cl/6870052/ proposal
and filepath.Walk.

-- if an iterator: return either a line OR an error, but never both?

-- auto-strip line endings before giving it to the caller?

-- CRLF vs LF

-- treat the final line without line endings the same as a line with a line ending?

-- skip, fail early, or truncate lines that are longer than the bufio Reader size?

-- string or []byte? (copy or not?)

etc.
@robpike
Copy link
Contributor

robpike commented Feb 14, 2013

Comment 1:

Status changed to Started.

@mewmew
Copy link
Contributor

mewmew commented Feb 15, 2013

Comment 2:

Personally I would feel that an iterator would be more convenient when reading lines.
The error could be similar to that of io.ReadFull:
"The error is EOF only if no bytes were read. If an EOF happens after reading some but
not all the bytes, ReadFull returns ErrUnexpectedEOF. On return, n == len(buf) if and
only if err == nil."
Return ErrUnexpectedEOF (or similar) if the line didn't end with a new line. Only return
EOF when no line was read.
Possibly return an error for truncated lines, ErrLineTruncated (or similar).
Line rog already pointed out: "You almost always want to strip off the final newline"
ref: https://golang.org/cl/6870052/#msg8
If a copy should be made or not is a hard choice. Can't tell at this point what my
thoughts are. It is always easy to provide a wrapper function to make the copy. The
question is, do we want to limit the need of such wrapper functions?

@mewmew
Copy link
Contributor

mewmew commented Feb 15, 2013

Comment 3:

When the line is too long (truncated) the error could be ErrPrefix to mimic the behavior
of the isPrefix bool.

@bradfitz
Copy link
Contributor Author

Comment 4:

Re comment #3: that would be terrible. It shouldn't be possible to accidentally get a
partial line in a convenience API.  If you care about partial lines you can use the
existing bufio.Reader.

@robpike
Copy link
Contributor

robpike commented Feb 20, 2013

Comment 5:

This issue was closed by revision 55ad7b9.

Status changed to Fixed.

@rsc rsc added this to the Go1.1 milestone Apr 14, 2015
@rsc rsc removed the go1.1 label Apr 14, 2015
@golang golang locked and limited conversation to collaborators Jun 24, 2016
@rsc rsc unassigned robpike Jun 22, 2022
This issue was closed.
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

5 participants