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

fmt: unexpected newline error #10853

Closed
syst3mw0rm opened this issue May 14, 2015 · 4 comments
Closed

fmt: unexpected newline error #10853

syst3mw0rm opened this issue May 14, 2015 · 4 comments
Milestone

Comments

@syst3mw0rm
Copy link
Contributor

The following program crashes with panic:

package main

import (
    "fmt"
    "bytes"
)

func main() {
    var a, b int
    r := bytes.NewReader([]byte{'1', '\n', '2'})

    _, err := fmt.Fscanf(r, "%d", &a)
    if err != nil {
        panic(err)
    }

    _, err = fmt.Fscanf(r, "%d", &b)
    if err != nil {
        panic(err)
    }
}
panic: unexpected newline

goroutine 1 [running]:
main.main()
    /tmp/f.go:19 +0x33d
exit status 2

on commit b83b011

@c9s
Copy link
Contributor

c9s commented May 29, 2015

I think this behavior is correct. if you run the program with go1.4.2, the result is the same: unexpected newline

/usr/local/go/bin/go version
go version go1.4.2 darwin/amd64

/usr/local/go/bin/go run test.go
panic: unexpected newline

goroutine 1 [running]:
main.main()
    /Users/c9s/go/test.go:19 +0x3e0

goroutine 2 [runnable]:
runtime.forcegchelper()
    /usr/local/go/src/runtime/proc.go:90
runtime.goexit()
    /usr/local/go/src/runtime/asm_amd64.s:2232 +0x1

goroutine 3 [runnable]:
runtime.bgsweep()
    /usr/local/go/src/runtime/mgc0.go:82
runtime.goexit()
    /usr/local/go/src/runtime/asm_amd64.s:2232 +0x1

goroutine 4 [runnable]:
runtime.runfinq()
    /usr/local/go/src/runtime/malloc.go:712
runtime.goexit()
    /usr/local/go/src/runtime/asm_amd64.s:2232 +0x1
exit status 2

@robpike
Copy link
Contributor

robpike commented Jun 5, 2015

Working as intended. The first call to Fscanf reads the '1'. The second call expects another integer but instead encounters a newline. It's not well explained in the documentation (and that will be addressed, so I'm leaving this bug open for now), but if there is no newline in the pattern, scanning stops at a newline.

@robpike
Copy link
Contributor

robpike commented Jun 5, 2015

Duplicate of #9459

@robpike robpike closed this as completed Jun 5, 2015
@syst3mw0rm
Copy link
Contributor Author

It's not exactly duplicate of #9459

Explicitly mentioning about behaviour being different than corresponding C routines well explains it in documentation - CL10779.

@golang golang locked and limited conversation to collaborators Jun 25, 2016
@rsc rsc unassigned robpike Jun 23, 2022
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

5 participants