Update ch1-05.md

pull/201/head
EpicMo 2023-07-20 10:20:08 +08:00 committed by GitHub
parent 2decff99fd
commit 279919079a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 2 additions and 2 deletions

View File

@ -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)
@ -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和iohttp.Get函数是创建HTTP请求的函数如果获取过程没有出错那么会在resp这个结构体中得到访问的请求结果。resp的Body字段包括一个可读的服务器响应流。io.ReadAll函数从response中读取到全部内容将其结果保存在变量b中。resp.Body.Close关闭resp的Body流防止资源泄露Printf函数会将结果b写出到标准输出流中。
```
$ go build gopl.io/ch1/fetch