Joe
Joe

Reputation: 47619

fgetpos available in Go? Want to find File.Position

I have a File and I want to find the file offset/position, what would be fgetpos in stdio. I can't seem to find it in http://golang.org/pkg/io/. Do I have to count it myself or is there a build in method?

Upvotes: 21

Views: 11036

Answers (3)

Spilvergo
Spilvergo

Reputation: 76

this should work:

currentPosition, err := file.Seek(0, 1)

i read it from here:

https://cihanozhan.medium.com/file-operations-in-golang-292825c9fb3d

Upvotes: 0

k.r.
k.r.

Reputation: 1

If it translates to syscall lseek(), it is quite expensive and can end up with expensive performance penalty compared to C ftell(), for example.

Upvotes: -1

unwind
unwind

Reputation: 399881

You should be able to do a Seek() to 0 bytes from the current position, which returns the resulting position. I'm not 100% sure the result is the absolute position you're after, but I would expect it to be.

offset, err := f.Seek(0, io.SeekCurrent)
if err != nil {
    // handle error
}
// offset is the current position

Upvotes: 42

Related Questions