Seeking to a negative offset

223 views
Skip to first unread message

Runix

unread,
Jul 21, 2015, 11:40:36 AM7/21/15
to golan...@googlegroups.com
Hi, why the documentation says that (http://golang.org/pkg/io/#Seeker):

Seeking to a negative offset is an error. Seeking to any positive offset is legal, but the behavior of subsequent I/O operations on the underlying object is implementation-dependent.

In the example I get the last byte using a negative offset. Why is an error?
http://play.golang.org/p/HjZJ_bDvHq

James Bardin

unread,
Jul 21, 2015, 12:07:03 PM7/21/15
to golan...@googlegroups.com


On Tuesday, July 21, 2015 at 11:40:36 AM UTC-4, Runix wrote:

In the example I get the last byte using a negative offset. Why is an error?
http://play.golang.org/p/HjZJ_bDvHq

It's not an error. You're seeking to a positive offset within the file. 
It would be an error if the file was 0 bytes long. 

Runix

unread,
Jul 21, 2015, 12:51:19 PM7/21/15
to golan...@googlegroups.com
Excuse me, I asked if there was an error in the documentation?

вторник, 21 июля 2015 г., 21:07:03 UTC+5 пользователь James Bardin написал:

andrey mirtchovski

unread,
Jul 21, 2015, 1:06:16 PM7/21/15
to Runix, golang-nuts
> Excuse me, I asked if there was an error in the documentation?

there is no error in the documentation. think of "offset" not as the
name of the first argument but as a byte mark inside the file, which
is arrived at by combining the original offset and the "whence"
argument.

for a file of length 10 bytes f.Seek(-1, os.SEEK_END) == f.Seek(10, os.SEEK_SET)

Keith Rarick

unread,
Jul 21, 2015, 2:10:11 PM7/21/15
to Runix, golang-nuts
On Tue, Jul 21, 2015 at 8:40 AM Runix <rsu...@gmail.com> wrote:
>
> Hi, why the documentation says that (http://golang.org/pkg/io/#Seeker):
>
> Seeking to a negative offset is an error. Seeking to any positive offset is legal, but the behavior of subsequent I/O operations on the underlying object is implementation-dependent.
>

I agree, that statement is ambiguous in context. The name of the first
parameter is "offset". The return value is also an offset, always
relative to the origin of the file, and it is an error to attempt to
seek to a position that would result in a negative value for this
returned offset. This could be expressed more clearly in the
commentary.

Brad Fitzpatrick

unread,
Jul 21, 2015, 11:19:31 PM7/21/15
to Keith Rarick, Runix, golang-nuts
Keith, want to file a bug?



--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Keith Rarick

unread,
Jul 27, 2015, 12:23:23 AM7/27/15
to Brad Fitzpatrick, Runix, golang-nuts
On Tue, Jul 21, 2015 at 8:19 PM Brad Fitzpatrick <brad...@golang.org> wrote:
> Keith, want to file a bug?

Done
https://github.com/golang/go/issues/11877
Reply all
Reply to author
Forward
0 new messages