On 22/09/22, 'Dan Kortschak' via golang-nuts (
golan...@googlegroups.com) wrote:
> On Thu, 2022-09-22 at 00:58 +0100, Rory Campbell-Lange wrote:
> > interface conversion: *zip.checksumReader is not io.ReadSeeker:
> > missing method Seek
>
> Would it be acceptable to conditionally copy the reader's contents into
> a buffer that implements io.ReadSeeker?
>
> if rs, ok := r.(io.ReadSeeker); ok {
> useReadSeeker(rs)
> } else {
> b, err := io.ReadAll(r)
> // handle err
> useReadSeeker(bytes.NewReader(b))
> }
Thanks very much for the suggestion. The following works ok as a test (which is basically your code):
fileAsReadSeeker := func(file fs.File) io.ReadSeeker {
if rs, ok := file.(io.ReadSeeker); ok {
return rs
}
b, err := io.ReadAll(file)
if err != nil {
panic(err) // to fix
}
return bytes.NewReader(b)
}
I'm now thinking of adding in a bytes.Buffer field into the underlying struct holding the fs.File file which can be initialised (and reused) if an io.ReadSeeker is required and provides a clearer error path.
Thanks again,
Rory