本文介绍了fgetpos在Go中可用吗?想要找到File.Position的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个File,我想查找文件的偏移量/位置,即stdio中的fgetpos.我似乎在 http://golang.org/pkg/io/中找不到它.我必须自己计算还是有内置方法?

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?

推荐答案

您应该能够从当前位置开始将Seek()设为0字节,这将返回结果位置.我不能100%地确定结果是否是您追求的绝对位置,但我希望它是绝对位置.

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

这篇关于fgetpos在Go中可用吗?想要找到File.Position的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 22:37