I found that, for two kind of reader: gzip.Reader
and bytes.Reader
, they have differently behavior on reading the last bytes, maybe I should create an issue on github?
Detail on following
For gzip.Reader
, it return 1 and an EOF for last byte read, but for bytes.Reader
, it return 1 and nil(error). I write this codes: Go Playground - The Go Programming Language
a function like this
func compareReader(r, b io.Reader) error {
var bufa [1]byte
var bufb [1]byte
for {
na, erra := r.Read(bufa[:])
nb, errb := b.Read(bufb[:])
if erra == nil && errb == nil && na == nb && bufa[0] == bufb[0] {
continue
}
if erra == errb && erra == io.EOF {
return nil
}
if erra != nil {
if erra == io.EOF && errb != io.EOF {
return fmt.Errorf("reader b has more data than a")
}
return fmt.Errorf("read on a error: %s", erra)
}
if errb != nil {
if errb == io.EOF && erra != io.EOF {
return fmt.Errorf("reader a has more data than b")
}
return fmt.Errorf("read on b error: %s", erra)
}
return nil
}
}
I don’t care about any thing like POSIX , linux man page, or any other, I just think that all std lib should be treated according to uniform standards.
And because the caller only know that this is an io.Reader, he doesn’t care the underlying implement of this reader.