mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2025-12-20 04:34:20 +08:00
rebuild
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
|
||||
<meta charset="UTF-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
|
||||
<title>穫取URL | Go编程语言</title>
|
||||
<title>獲取URL | Go编程语言</title>
|
||||
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
|
||||
<meta name="description" content="">
|
||||
<meta name="generator" content="GitBook 2.5.2">
|
||||
@@ -21,6 +21,10 @@
|
||||
<link rel="stylesheet" href="../gitbook/style.css">
|
||||
|
||||
|
||||
<link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-katex/katex.min.css">
|
||||
|
||||
|
||||
|
||||
<link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
|
||||
|
||||
|
||||
@@ -44,7 +48,7 @@
|
||||
<body>
|
||||
|
||||
|
||||
<div class="book" data-level="1.5" data-chapter-title="穫取URL" data-filepath="ch1/ch1-05.md" data-basepath=".." data-revision="Fri Dec 25 2015 12:32:44 GMT+0800 (中国标准时间)">
|
||||
<div class="book" data-level="1.5" data-chapter-title="獲取URL" data-filepath="ch1/ch1-05.md" data-basepath=".." data-revision="Mon Dec 28 2015 16:03:52 GMT+0800 (中国标准时间)">
|
||||
|
||||
|
||||
<div class="book-summary">
|
||||
@@ -238,7 +242,7 @@
|
||||
|
||||
<b>1.5.</b>
|
||||
|
||||
穫取URL
|
||||
獲取URL
|
||||
</a>
|
||||
|
||||
|
||||
@@ -253,7 +257,7 @@
|
||||
|
||||
<b>1.6.</b>
|
||||
|
||||
併發穫取多個URL
|
||||
併發獲取多個URL
|
||||
</a>
|
||||
|
||||
|
||||
@@ -802,7 +806,7 @@
|
||||
|
||||
<b>5.10.</b>
|
||||
|
||||
Recover捕穫異常
|
||||
Recover捕獲異常
|
||||
</a>
|
||||
|
||||
|
||||
@@ -1315,7 +1319,7 @@
|
||||
|
||||
<b>8.9.</b>
|
||||
|
||||
併發的退齣
|
||||
併發的退出
|
||||
</a>
|
||||
|
||||
|
||||
@@ -1834,7 +1838,7 @@
|
||||
|
||||
<b>12.7.</b>
|
||||
|
||||
穫取結構體字段標識
|
||||
獲取結構體字段標識
|
||||
</a>
|
||||
|
||||
|
||||
@@ -2019,9 +2023,9 @@
|
||||
|
||||
<section class="normal" id="section-">
|
||||
|
||||
<h2 id="15-穫取url">1.5. 穫取URL</h2>
|
||||
<h2 id="15-獲取url">1.5. 獲取URL</h2>
|
||||
<p>對於很多現代應用來説,訪問互聯網上的信息和訪問本地文件繫統一樣重要。Go語言在net這個強大package的幫助下提供了一繫列的package來做這件事情,使用這些包可以更簡單地用網絡收發信息,還可以建立更底層的網絡連接,編寫服務器程序。在這些情景下,Go語言原生的併發特性(在第八章中會介紹)就顯得尤其好用了。</p>
|
||||
<p>爲了最簡單地展示基於HTTP穫取信息的方式,下面給齣一個示例程序fetch,這個程序將穫取對應的url,併將其源文本打印齣來;這個例子的靈感來源於curl工具(譯註:unix下的一個網絡相關的工具)。當然了,curl提供的功能更爲複雜豐富,這里我們隻編寫最簡單的樣例。之後我們還會在本書中經常用到這個例子。</p>
|
||||
<p>爲了最簡單地展示基於HTTP獲取信息的方式,下面給出一個示例程序fetch,這個程序將獲取對應的url,併將其源文本打印出來;這個例子的靈感來源於curl工具(譯註:unix下的一個網絡相關的工具)。當然了,curl提供的功能更爲複雜豐富,這里我們隻編寫最簡單的樣例。之後我們還會在本書中經常用到這個例子。</p>
|
||||
<pre><code class="lang-go">gopl.io/ch1/fetch
|
||||
<span class="hljs-comment">// Fetch prints the content found at a URL.</span>
|
||||
<span class="hljs-keyword">package</span> main
|
||||
@@ -2050,7 +2054,7 @@
|
||||
}
|
||||
}
|
||||
</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/ioutil包,http.Get函數是創建HTTP請求的函數,如果獲取過程沒有出錯,那麽會在resp這個結構體中得到訪問的請求結果。resp的Body字段包括一個可讀的服務器響應流。這之後ioutil.ReadAll函數從response中讀取到全部內容;其結果保存在變量b中。resp.Body.Close這一句會關閉resp的Body流,防止資源洩露,Printf函數會將結果b寫出到標準輸出流中。</p>
|
||||
<pre><code>$ go build gopl.io/ch1/fetch
|
||||
$ ./fetch http://gopl.io
|
||||
<html>
|
||||
@@ -2066,7 +2070,7 @@ fetch: Get http://gopl.io: dial tcp: lookup gopl.io: getaddrinfow: No such host
|
||||
</code></pre><p>無論哪種失敗原因,我們的程序都用了os.Exit函數來終止進程,併且返迴一個status錯誤碼,其值爲1。</p>
|
||||
<p><strong>練習 1.7:</strong> 函數調用io.Copy(dst, src)會從src中讀取內容,併將讀到的結果寫入到dst中,使用這個函數替代掉例子中的ioutil.ReadAll來拷貝響應結構體到os.Stdout,避免申請一個緩衝區(例子中的b)來存儲。記得處理io.Copy返迴結果中的錯誤。</p>
|
||||
<p><strong>練習 1.8:</strong> 脩改fetch這個范例,如果輸入的url參數沒有 <code>http://</code> 前綴的話,爲這個url加上該前綴。你可能會用到strings.HasPrefix這個函數。</p>
|
||||
<p><strong>練習 1.9:</strong> 脩改fetch打印齣HTTP協議的狀態碼,可以從resp.Status變量得到該狀態碼。</p>
|
||||
<p><strong>練習 1.9:</strong> 脩改fetch打印出HTTP協議的狀態碼,可以從resp.Status變量得到該狀態碼。</p>
|
||||
|
||||
|
||||
</section>
|
||||
@@ -2080,7 +2084,7 @@ fetch: Get http://gopl.io: dial tcp: lookup gopl.io: getaddrinfow: No such host
|
||||
<a href="../ch1/ch1-04.html" class="navigation navigation-prev " aria-label="Previous page: GIF動畵"><i class="fa fa-angle-left"></i></a>
|
||||
|
||||
|
||||
<a href="../ch1/ch1-06.html" class="navigation navigation-next " aria-label="Next page: 併發穫取多個URL"><i class="fa fa-angle-right"></i></a>
|
||||
<a href="../ch1/ch1-06.html" class="navigation navigation-next " aria-label="Next page: 併發獲取多個URL"><i class="fa fa-angle-right"></i></a>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
@@ -2098,7 +2102,7 @@ fetch: Get http://gopl.io: dial tcp: lookup gopl.io: getaddrinfow: No such host
|
||||
|
||||
<script>
|
||||
require(["gitbook"], function(gitbook) {
|
||||
var config = {"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
|
||||
var config = {"katex":{},"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
|
||||
gitbook.start(config);
|
||||
});
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user