mirror of
				https://github.com/gopl-zh/gopl-zh.github.com.git
				synced 2025-11-04 03:41:37 +00:00 
			
		
		
		
	deploy: d948a26c96
				
					
				
			This commit is contained in:
		@@ -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和io,http.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
 | 
			
		||||
<html>
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user