Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

第一章 1. 1.2 ioutil — 方便的IO操作函数集 #91

Open
heweshub opened this issue May 11, 2022 · 0 comments
Open

第一章 1. 1.2 ioutil — 方便的IO操作函数集 #91

heweshub opened this issue May 11, 2022 · 0 comments

Comments

@heweshub
Copy link

heweshub commented May 11, 2022

1.2. ReadAll 函数
很多时候,我们需要一次性读取 io.Reader 中的数据,通过上一节的讲解,我们知道有很多种实现方式。考虑到读取所有数据的需求比较多,Go 提供了 ReadAll 这个函数,用来从io.Reader 中一次读取所有数据。

func ReadAll(r io.Reader) ([]byte, error)

阅读该函数的源码发现,它是通过 bytes.Buffer 中的 ReadFrom 来实现读取所有数据的。该函数成功调用后会返回 err == nil 而不是 err == EOF。(成功读取完毕应该为 err == io.EOF,这里返回 nil 由于该函数成功期望 err == io.EOF,符合无错误不处理的理念)
这里的ReadAll方法是直接使用了Read方法,而不是ReadFrom,请仔细阅读源码
而且是直接调用io包中的readAll

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant