gh-pages
github-actions[bot] 2023-07-22 12:28:52 +00:00
parent 619d6d3117
commit cf20975227
4 changed files with 6 additions and 6 deletions

View File

@ -181,7 +181,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)
@ -191,7 +191,7 @@ func main() {
}
}
</code></pre>
<p>这个程序从两个package中导入了函数net/http和io/ioutil包http.Get函数是创建HTTP请求的函数如果获取过程没有出错那么会在resp这个结构体中得到访问的请求结果。resp的Body字段包括一个可读的服务器响应流。ioutil.ReadAll函数从response中读取到全部内容将其结果保存在变量b中。resp.Body.Close关闭resp的Body流防止资源泄露Printf函数会将结果b写出到标准输出流中。</p>
<p>这个程序从两个package中导入了函数net/http和iohttp.Get函数是创建HTTP请求的函数如果获取过程没有出错那么会在resp这个结构体中得到访问的请求结果。resp的Body字段包括一个可读的服务器响应流。io.ReadAll函数从response中读取到全部内容将其结果保存在变量b中。resp.Body.Close关闭resp的Body流防止资源泄露Printf函数会将结果b写出到标准输出流中。</p>
<pre><code>$ go build gopl.io/ch1/fetch
$ ./fetch http://gopl.io
&lt;html&gt;

View File

@ -622,7 +622,7 @@ func main() {
fmt.Fprintf(os.Stderr, &quot;fetch: %v\n&quot;, 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, &quot;fetch: reading %s: %v\n&quot;, url, err)
@ -632,7 +632,7 @@ func main() {
}
}
</code></pre>
<p>这个程序从两个package中导入了函数net/http和io/ioutil包http.Get函数是创建HTTP请求的函数如果获取过程没有出错那么会在resp这个结构体中得到访问的请求结果。resp的Body字段包括一个可读的服务器响应流。ioutil.ReadAll函数从response中读取到全部内容将其结果保存在变量b中。resp.Body.Close关闭resp的Body流防止资源泄露Printf函数会将结果b写出到标准输出流中。</p>
<p>这个程序从两个package中导入了函数net/http和iohttp.Get函数是创建HTTP请求的函数如果获取过程没有出错那么会在resp这个结构体中得到访问的请求结果。resp的Body字段包括一个可读的服务器响应流。io.ReadAll函数从response中读取到全部内容将其结果保存在变量b中。resp.Body.Close关闭resp的Body流防止资源泄露Printf函数会将结果b写出到标准输出流中。</p>
<pre><code>$ go build gopl.io/ch1/fetch
$ ./fetch http://gopl.io
&lt;html&gt;

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long