请介绍 Go 语言中 io.Reader 和 io.Writer 接口的核心约定,以及 io.Read 与 io.Write 方法在实现和调用时的注意事项。
考察说明
考察对 Go I/O 接口契约的理解及边界情况处理
回答思路
- 准确说明 Read 返回 n 与 err 的语义,包括 n>0 且 err=EOF 的情况
- 说明 Write 应返回非 nil error 当 n<len(p),以及 n>0 时不应返回 nil error
- 讨论 io.EOF、io.ErrUnexpectedEOF、io.ErrShortWrite 等错误约定
- 能解释一次性读取与循环读取的正确用法