Go语言中Reader接口定义了一个Read操作,实现了Reader接口的都有Read这个函数,接口的定义如下:
type Reader interface {
Read(p []byte) (n int, err error)
}
在实际使用的时候可能会对一个参数和这两个返回值有疑惑,什么时候返回什么值什么的,特别是类似于网络IO这种下面利用官网的文档说明一下。
Reader is the interface that wraps the basic Read method.
Read reads up to len(p) bytes into p. It returns the number of bytes read (0 <= n <= len(p)) and any error encountered. Even if Read returns n < len(p), it may use all of p as scratch space during the call. If some data is available but not len(p) bytes, Read conventionally returns what is available instead of waiting for more.
When Read encounters an error or end-of-file con