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: document that ReadLine ignores missing \n at EOF #3825

Closed
nsf opened this issue Jul 14, 2012 · 6 comments
Closed

bufio: document that ReadLine ignores missing \n at EOF #3825

nsf opened this issue Jul 14, 2012 · 6 comments
Milestone

Comments

@nsf
Copy link

nsf commented Jul 14, 2012

Consider this test:
------------------------------------------------------------------
package main

import "bufio"
import "strings"
import "fmt"

func read_lines(data string) {
        fmt.Printf("reading %q\n", data)
        br := bufio.NewReader(strings.NewReader(data))
        for {
                line, is_prefix, err := br.ReadLine()
                fmt.Println(line, is_prefix, err)
                if err != nil {
                        fmt.Println()
                        return
                }
        }
}

func main() {
        read_lines("123\n\n123")
        read_lines("123\n\n123\n")
}
------------------------------------------------------------------

The output for this test is:
------------------------------------------------------------------
reading "123\n\n123"
[49 50 51] false <nil>
[] false <nil>
[49 50 51] false <nil>
[] false EOF

reading "123\n\n123\n"
[49 50 51] false <nil>
[] false <nil>
[49 50 51] false <nil>
[] false EOF
------------------------------------------------------------------

As you can see it is identical for both cases. The problem is that in second case, the
last empty line gets ignored. However in general ReadLine doesn't ignore empty lines.
The question is: is it a special case? If yes - it must be documented. If no - it is a
bug.

In my opinion it is a bug. Since ReadLine returns non-empty lines which do not end with
EOL symbol, it must return empty lines as well.

Won't go into details why it happens, should be pretty obvious from the ReadLine's
source code.
@mxk
Copy link

mxk commented Jul 16, 2012

Comment 1:

The second case is handled correctly; it's the first case that may be a bit surprising.
The last line is incomplete (missing LF), but the documentation for ReadLine states:
"ReadLine either returns a non-nil line or it returns an error, never both."
As a result, if an incomplete line is followed by EOF, ReadLine has to return whatever
it managed to find without any error indication. For this and some other reasons, I
prefer to use ReadSlice('\n') instead and do my own line ending checks.

@gopherbot
Copy link

Comment 2 by ikkwong@google.com:

Please have a look at this: http://play.golang.org/p/Yb3Alqee25
The there is an extra first line with ReadLine and missing last empty line.

@mxk
Copy link

mxk commented Aug 24, 2012

Comment 3:

Your for loop performs the first iteration before ReadLine is called. Here's the
corrected version, which works as expected: http://play.golang.org/p/wCL4JX5eWv

@rsc
Copy link
Contributor

rsc commented Sep 12, 2012

Comment 4:

Labels changed: added priority-later, removed priority-triage.

Status changed to Accepted.

@rsc
Copy link
Contributor

rsc commented Sep 12, 2012

Comment 5:

Labels changed: added go1.1.

@rsc
Copy link
Contributor

rsc commented Dec 10, 2012

Comment 6:

This issue was closed by revision f0d9ccb.

Status changed to Fixed.

@nsf nsf added fixed labels Dec 10, 2012
@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
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

4 participants