萌新请问一下,怎样判断这个con.read()是否读完了,我点进去看了一下,读完,长度n应该返回会0,但是结果和我想不一样,求大佬帮忙解答一下
![XXQVC4D}C(M0N_)RE3FUK{D.png](https://static.studygolang.com/180905/e43156fcaaf5f8517bf279d0ddba3238.png)
更多评论
你到底要干啥。
如果是读文件或者copy的话,一般用io.copy或者iotuils.ReadAll就可以了。
否则的话,应该是判断错误是否是 io.EOF
#2
关于io.reader的说明
Reader is the interface that wraps the basic Read method.
https://golang.org/pkg/io/#Reader
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 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. An instance of this general case is that a Reader returning a non-zero number of bytes at the end of the input stream may return either err == EOF or err == nil. The next Read should return 0, EOF.
Callers should always process the n > 0 bytes returned before considering the error err. Doing so correctly handles I/O errors that happen after reading some bytes and also both of the allowed EOF behaviors.
Implementations of Read are discouraged from returning a zero byte count with a nil error, except when len(p) == 0. Callers should treat a return of 0 and nil as indicating that nothing happened; in particular it does not indicate EOF.
#3