binary.Read behaves (almost) like io.ReadFull

323 views
Skip to first unread message

yy

unread,
Dec 17, 2010, 10:42:02 AM12/17/10
to golang-nuts
I was trying to use binary.Read to read a slice of int32 from a file.
The slice has a given length, which in most cases will leave some free
space at the end. I thought this should work:

data := make([]int32, size)
err := binary.Read(file, binary.LittleEndian, data)

but this fails with io.ErrUnexpectedEOF when file is smaller than
size. If this is intended, I'd suggest to rename the function to
ReadFull and fill data as much as we can before returning the error,
as io.ReadFull does. If there is no need to fail here, it would be
even simpler to forget about io.ReadFull and just use the Read method
directly.

Whatever is the best solution I have no problem to send the CL (and,
while I'm into it, send another one to fix issue 1305). Anyway, I
think the documentation should be clearer about what happens when
binary.Read finds EOF. I needed to look at binary.go to understand
what was going on.

--
- yiyus || JGL . 4l77.com

peterGo

unread,
Dec 17, 2010, 11:13:28 AM12/17/10
to golang-nuts
yy,


Issue 1305: encoding/binary Read and Write should return amount of
bytes read or written
http://code.google.com/p/go/issues/detail?id=1305

Peter
Reply all
Reply to author
Forward
0 new messages