Skip to content

bufio: ScanLines does not scan os.Stdin if line is more than 65536 chars #11242

@alex-netkachov

Description

@alex-netkachov

The following code fails to read the line passed to stdin if the line is more too long:

package main

import (
    "bufio"
    "fmt"
    "os"
)

func main() {
    s := bufio.NewScanner(bufio.NewReader(os.Stdin))
    s.Split(bufio.ScanLines)
    s.Scan()
    fmt.Printf("%v\n", len(s.Text()))
}

The expected output is line length, the actual output is 0. golang-issue.txt is file with single very long line. If the line is 65536 or less this code works ok, if it has more characters, the code fails.

Surprisingly, the second call to Scan() returns the complete line.

  1. go version go1.4.2 darwin/amd64
  2. OSX 10.10.3 (14D136)
  3. Program is started as follows: go fmt golang-issue.go && go run golang-issue.go < golang-issue.txt

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions