golang io需要牢记的几个点

时间:2022-11-18 21:11:16

对于Reader比较麻烦需要记住以下:

When Read encounters an error or end-of-file condition after successfully reading n > 0 bytes, it returns the number of bytes read. It may return the (non-nil) error from the same call or return the error (and n == 0) from a subsequent call

对于Write比较简单需要记住一下:

Write writes len(p) bytes from p to the underlying data stream. It returns the number of bytes written from p (0 <= n <= len(p)) and any error encountered that caused the write to stop early. Write must return a non-nil error if it returns n < len(p). Write must not modify the slice data, even temporarily.

对于ByteReader:就是一个字节一个字节的读

buio是对io的上层封装,他处理了一些常规错误情况等,另外读取支持读取到遇到某个字节为止,等等,还有一些其他很常用的功能。