Compare commits

...

3 Commits

Author SHA1 Message Date
EpicMo 39a364606e
Merge 213fe7cbaa into d948a26c96 2023-07-24 17:46:08 +08:00
chai2010 d948a26c96
Merge pull request #201 from liaosunny123/patch-1
Update ch1-05.md
2023-07-22 20:28:37 +08:00
EpicMo 279919079a
Update ch1-05.md 2023-07-20 10:20:08 +08:00
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