Skip to content

Commit

Permalink
Update ch1-05.md
Browse files Browse the repository at this point in the history
  • Loading branch information
liaosunny123 committed Jul 20, 2023
1 parent 2decff9 commit 2799190
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions ch1/ch1-05.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func main() {
fmt.Fprintf(os.Stderr, "fetch: %v\n", err)
os.Exit(1)
}
b, err := ioutil.ReadAll(resp.Body)
b, err := io.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
fmt.Fprintf(os.Stderr, "fetch: reading %s: %v\n", url, err)
Expand All @@ -34,7 +34,7 @@ func main() {
}
```

这个程序从两个package中导入了函数,net/http和io/ioutil包,http.Get函数是创建HTTP请求的函数,如果获取过程没有出错,那么会在resp这个结构体中得到访问的请求结果。resp的Body字段包括一个可读的服务器响应流。ioutil.ReadAll函数从response中读取到全部内容;将其结果保存在变量b中。resp.Body.Close关闭resp的Body流,防止资源泄露,Printf函数会将结果b写出到标准输出流中。
这个程序从两个package中导入了函数,net/http和io,http.Get函数是创建HTTP请求的函数,如果获取过程没有出错,那么会在resp这个结构体中得到访问的请求结果。resp的Body字段包括一个可读的服务器响应流。io.ReadAll函数从response中读取到全部内容;将其结果保存在变量b中。resp.Body.Close关闭resp的Body流,防止资源泄露,Printf函数会将结果b写出到标准输出流中。

```
$ go build gopl.io/ch1/fetch
Expand Down

0 comments on commit 2799190

Please sign in to comment.