Skip to content

Commit

Permalink
Correct a typo
Browse files Browse the repository at this point in the history
`NopCloser` was misspelled as `NoCloser`
  • Loading branch information
carsonip authored Aug 22, 2016
1 parent 999f0cb commit c45a07b
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions chapter01/01.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

有时候我们需要传递一个io.ReadCloser的实例,而我们现在有一个io.Reader的实例,比如:strings.Reader,这个时候NopCloser就派上用场了。它包装一个io.Reader,返回一个io.ReadCloser,而相应的Close方法啥也不做,只是返回nil。

比如,在标准库net/http包中的NewRequest,接收一个io.Reader的body,而实际上,Request的Body的类型是io.ReadCloser,因此,代码内部进行了判断,如果传递的io.Reader也实现了io.ReadCloser接口,则转换,否则通过ioutil.NoCloser包装转换一下。相关代码如下:
比如,在标准库net/http包中的NewRequest,接收一个io.Reader的body,而实际上,Request的Body的类型是io.ReadCloser,因此,代码内部进行了判断,如果传递的io.Reader也实现了io.ReadCloser接口,则转换,否则通过ioutil.NopCloser包装转换一下。相关代码如下:

rc, ok := body.(io.ReadCloser)
if !ok && body != nil {
Expand Down Expand Up @@ -140,4 +140,4 @@ devNull 在实现io.Writer接口时,只是简单的返回(标准库文件:

- [目录](/preface.md)
- 上一节:[io — 基本的IO接口](01.1.md)
- 下一节:[fmt — 格式化IO](01.3.md)
- 下一节:[fmt — 格式化IO](01.3.md)

0 comments on commit c45a07b

Please sign in to comment.