nums := make([]int, 0)
{
var d int
for {
_, err := fmt.Scan(&d)
if err != nil {
break
}
nums = append(nums, d)
}
}
This works to read in ints from stdin. But if stdin looks like 1 2 3 f4 5
, nums
will end up being [1 2 3]
without reporting any error.
What's the best way of handling this? (Ie, I want EOF from fmt.Scan
to silently exit the loop, but all other errors should be reported).
edit: io.EOF
was all I needed -- I hadn't found that documented at http://golang.org/pkg/fmt/
nums := make([]int, 0)
{
var d int
for {
_, err := fmt.Scan(&d)
if err != nil {
if err != io.EOF {
log.Fatal(err)
}
break
}
nums = append(nums, d)
}
}