实现了 Seek 方法的流

huangapple go评论115阅读模式
英文:

Stream which implements a Seek method

问题

我正在尝试找到一个接口,它允许我创建一个流,可以从文件或[]byte中进行寻址(只有一个Reader也可以),但是在godoc中似乎找不到任何相关内容。bufio包中的一些类型可能很适合,但它们似乎不支持寻址。

我是否忽略了某些符合我需求的内容?

英文:

I'm trying to find an interface which allows me to create a stream which allows seeking (just a Reader is fine, too) from either a file or []byte, but can't seem to find anything in the godoc. Some of the types in the bufio package would work quite well, but they don't appear to support seeking.

Is there something I overlooked which would fit what I'm looking for?

答案1

得分: 4

*os.File(用于文件)和*bytes.Reader(用于从[]byte获取io.Reader)都实现了io.Seeker接口,因此具有Seek方法。

io.Seeker的实现包括...
*bytes.Reader
*io.SectionReader
io.ReadSeeker
io.WriteSeeker
io.ReadWriteSeeker
mime/multipart.File
net/http.File
*os.File
*strings.Reader

因此,如果您正在处理文件,很可能是*os.File,则无需执行任何其他操作即可进行寻址。只需确保如果您使用的是接口而不是具体类型,则不要使用io.Reader而是使用io.ReadSeeker

英文:

Both *os.File (for files) and *bytes.Reader (for having an io.Reader from a []byte) implement the io.Seeker interface and thus have a Seek method.

io.Seeker is implemented by...
        *bytes.Reader
        *io.SectionReader
        io.ReadSeeker
        io.WriteSeeker
        io.ReadWriteSeeker
        mime/multipart.File
        net/http.File
        *os.File
        *strings.Reader

So if you're working with a file, thus very likely *os.File, you don't need to do anything additional to be able to seek it. Just make sure that if you're using interfaces instead of concrete types that you do not want an io.Reader but an io.ReadSeeker.

huangapple
  • 本文由 发表于 2013年6月28日 02:16:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/17350423.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定