Skip to content

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

@nsf

Description

@nsf
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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions