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

Partial Line returned from Reader.ReadLine() #28718

Closed
jason-baker opened this issue Nov 10, 2018 · 2 comments
Closed

Partial Line returned from Reader.ReadLine() #28718

jason-baker opened this issue Nov 10, 2018 · 2 comments

Comments

@jason-baker
Copy link

@jason-baker jason-baker commented Nov 10, 2018

What version of Go are you using (go version)?

$ go version
go version go1.10.2 windows/amd64

Does this issue reproduce with the latest release?

Yes

$ go version
go version go1.11.2 windows/amd64

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\\AppData\Local\go-build
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Users\\go
set GORACE=
set GOROOT=C:\Go
set GOTMPDIR=
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\\AppData\Local\Temp\go-build034252242=/tmp/go-build -gno-record-gcc-switches

What did you do?

func main() {
	reader := bufio.NewReaderSize(os.Stdin, 1024*1024)

	qTemp, err := strconv.ParseInt(readLine(reader), 10, 64)
	checkError(err)
	q := int32(qTemp)

	for qItr := 0; qItr < int(q); qItr++ {
		nm := strings.Split(readLine(reader), " ")

		nTemp, err := strconv.ParseInt(nm[0], 10, 64)
		checkError(err)
		n := int32(nTemp)

		m, err := strconv.ParseInt(nm[1], 10, 64)
		checkError(err)

		println("n: ", n, "m: ", m)

		aTemp := strings.Split(readLine(reader), " ")
		println(len(aTemp))
		if int32(len(aTemp)) != n {
			println(fmt.Sprintf("%v", strings.Split(readLine(reader), " ")))
		}

		var a []int64

		for i := 0; i < int(n); i++ {
			aItem, err := strconv.ParseInt(aTemp[i], 10, 64)
			checkError(err)
			a = append(a, aItem)
		}

		result := maximumSum(a, m)

		println(fmt.Sprintf("%d\n", result))
	}
}

func readLine(reader *bufio.Reader) string {
	str, _, err := reader.ReadLine()
	println(err)
	if err == io.EOF {
		return ""
	}

	return strings.TrimRight(string(str), "\r\n")
}

test.txt

cat test.txt | go run test.go

What did you expect to see?

All bytes returned on one line

What did you see instead?

cat test.txt | go run test.go
n: 100000 m: 10002143548612
100000
n: 100000 m: 10000418366442
100000
n: 100000 m: 10001238194497
100000
n: 100000 m: 10000855912503
99993
panic: Unexpeced return from readline

goroutine 1 [running]:
main.main()
C:/Users//go/src/test.go:33 +0x51b
exit status 2

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Nov 10, 2018

Please read the docs for Reader.ReadLine (https://golang.org/pkg/bufio/#Reader.ReadLine). They are clear that ReadLine does not always return a complete line. You need to either check the isPrefix result, which you are currently ignoring, or use ReadBytes.

@jason-baker

This comment has been minimized.

Copy link
Author

@jason-baker jason-baker commented Nov 10, 2018

Mea culpa; you are correct this is a non-issue.

@golang golang locked and limited conversation to collaborators Nov 10, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
3 participants
You can’t perform that action at this time.