You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Scanf doesn't seem to care about newline in format. It's also putting an extraneous \n in err.Error() if scanned input is longer than expected.
func main() {
var input int
_, err := fmt.Scanf("%d\n", &input)
if err != nil {
fmt.Printf("%q", err)
}
}
With "golang" as input, the "olang" gets dragged to a newline in err. This is contrary to what the documentation says:
Scanf, Fscanf and Sscanf require that (after skipping spaces) newlines in the format are matched by newlines in the input and vice versa.
With that proviso, text in the format string must match the input text; scanning stops if it does not, with the return value of the function indicating the number of arguments scanned
bash-3.2$ go run test.go
golang
"expected integer"bash-3.2$ olang
bash: olang: command not found
bash-3.2$
The text was updated successfully, but these errors were encountered:
bradfitz
changed the title
Scanf's err puts extraneous '\n' if arg is small
fmt: Scanf's err puts extraneous '\n' if arg is small
Dec 27, 2015
"olang" isn't in err. What's happening is fmt.Scanf is reading one byte at a time, it reads the 'g' character, realizes that's not an integer character, then your Go program exits after printing "expected integer". The remaining "olang" characters are still buffered in the terminal's input, and are read and interpreted by your shell.
@mdempsky That's what I realized after filing the bug. But what's odd is, it happens only if I print the err. It doesn't happen if I use input redirection, of course or if err is not printed at all!
Scanf doesn't seem to care about newline in format. It's also putting an extraneous
\n
inerr.Error()
if scanned input is longer than expected.With "golang" as input, the "olang" gets dragged to a newline in
err
. This is contrary to what the documentation says:The text was updated successfully, but these errors were encountered: