英文:
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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论