On 26 April 2012 05:07, Mehul Choube <
mch...@gmail.com> wrote:
> _, err := fmt.Fscanln(Stdin, &i)
Andrey pointed out what was going on here, but to be
a little more explicit, here's what the documentation says
(from
http://golang.org/pkg/fmt):
: The familiar base-setting prefixes 0 (octal) and 0x (hexadecimal) are
: accepted when scanning integers without a format or with the %v verb.
What's happening is the fmt is seeing the "0" prefix and
disallowing any digits outside the range [0-7].
Unfortunately there's no equivalent of Fscanln (e.g. Fscanfln) that
allows scanning decimal numbers only.
You could use bufio.ReadSlice to read lines explicitly and then use fmt.Sscanf.
Alternatively you could implement a type that implements decimal-only
scanning, and continue to use Fscanln, for instance:
http://play.golang.org/p/PjzywMIYzZ
(It seems a pity that when presented with EOF, fmt.Fscanln returns io.EOF but
fmt.Fscanf returns io.ErrUnexpectedEOF - hence the fact that the example above
prints "unexpected EOF" rather than just "EOF" at the end.