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: ScanLines does not scan os.Stdin if line is more than 65536 chars #11242

Closed
alex-netkachov opened this issue Jun 17, 2015 · 1 comment
Closed

Comments

@alex-netkachov
Copy link

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
@alex-netkachov
Copy link
Author

Sorry, just found the constant MaxScanTokenSize. So the workaround will be to use reader - http://stackoverflow.com/questions/21124327/how-to-read-a-text-file-line-by-line-in-go-when-some-lines-are-long-enough-to-ca

@mikioh mikioh changed the title ScanLines from bufio does not scan os.Stdin if line is more than 65536 chars bufio: ScanLines does not scan os.Stdin if line is more than 65536 chars Jun 17, 2015
@golang golang locked and limited conversation to collaborators Jun 25, 2016
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